layout_gravity和gravity区别

来源:互联网 发布:知柏地黄丸治口臭吗 编辑:程序博客网 时间:2024/05/16 07:54
layout_gravity 是控制组件在父组件中的位置
gravity 是控制组件的子组件/内容(文字)在组件中的位置


主要是注意 layout_gravity 在 LinearLayout(线性布局)中的应用
与线性布局的方向相关 对应属性 android:orientation="vertical|horizontal"
1.vertical 横向属性起作用 例如 left right center_horizontal
2.horizontal 纵向属性起作用 例如 top bottom center_vertical
注意 center 属性在俩种情况下都起作用


    <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#f00">


        <TextView
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:background="#0f0"
                android:text="Hello World"
                android:gravity="center"
                android:layout_gravity="right"
                />


    </LinearLayout>
    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:background="#00f">


        <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#0f0"
                android:text="Hello World"
                android:gravity="center"
                android:layout_gravity="bottom"
                />


    </LinearLayout>


在 FrameLayout(帧布局)可以叠加使用 例如 左上 - left|top


    <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="左上"
                android:layout_gravity="left|top"/>


    </FrameLayout>


源代码下载地址




0 0
原创粉丝点击