The following code shows how to Set layout weight, gravity and background.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffEEee" android:gravity="center" android:orientation="horizontal" android:weightSum="1" > <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.5" android:text="Click Me" /> </LinearLayout>
Java code
import android.app.Activity; import android.os.Bundle; /*w ww. ja v a 2 s. c o m*/ public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }