zoukankan      html  css  js  c++  java
  • 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的值月大,重要度越低。

  • 相关阅读:
    用户故事与敏捷方法阅读笔记2
    用户故事与敏捷方法阅读笔记1
    梦断代码阅读笔记3
    梦断代码阅读笔记2
    梦断代码阅读笔记1
    疫情可视化项目-进度2
    疫情可视化项目-进度1
    每日总结3.16
    Android体温记录器更新-进度5
    每日总结3.11
  • 原文地址:https://www.cnblogs.com/suinuaner/p/2268574.html
Copyright © 2011-2022 走看看