Layout_gravity,gravity和Layout_weight

2011-11-30

  对于Layout_gravity,gravity和Layout_weight,之前用时没在意,前两天用时出了点问题,就上网搜了一下,结果各抒己见,不统一。最后,自己亲自实践了一下(只关注属性结果,界面有点丑,请各位多多包涵哈),结果如下:

layout_gravity:本控件在它父控件中的相对位置

gravity:本控件中的内容在本控件中的相对位置

layout_weight如下:

java用一个就可以:

public class ActivityActivity extends Activity {
    /** Called when the activity is first created. */
 private Button mButton1=null;
 private Button mButton2=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mButton1=(Button)findViewById(R.id.btn1);
        mButton2=(Button)findViewById(R.id.btn2);
   
    }
}

有四种布局:

第一种

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button1"/>
<Button android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:text="Button2"/>

</LinearLayout>

效果图:

第二种:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button android:id="@+id/btn1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button1"/>
<Button android:id="@+id/btn2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:text="Button2"/>

</LinearLayout>

效果图:

第三种:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button1"/>
<Button android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:text="Button2"/>

</LinearLayout>

效果图

第四种:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button android:id="@+id/btn1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button1"/>
<Button android:id="@+id/btn2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:text="Button2"/>

</LinearLayout>

效果图:

总结:前三种情况:layout_weight的值越大,重要度越高;第四种情况:layout_weight的值月大,重要度越低。

原文地址:https://www.cnblogs.com/suinuaner/p/2268574.html