LinearLayout(线性布局)中weight的难点
2017-01-17 09:21
387 查看
一、首先我们先用 RelativeLayout (相对布局)实现一个效果,如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.administrator.myapplication.MainActivity"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/rg_but" ></FrameLayout> <RadioGroup android:id="@+id/rg_but" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="@null" android:text="1" android:drawableTop="@mipmap/ic_launcher" android:gravity="center_horizontal" android:layout_weight="1"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2" android:drawableTop="@mipmap/ic_launcher" android:gravity="center_horizontal" android:button="@null" android:layout_weight="1"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3" android:drawableTop="@mipmap/ic_launcher" android:gravity="center_horizontal" android:button="@null" android:layout_weight="1"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4" android:drawableTop="@mipmap/ic_launcher" android:gravity="center_horizontal" android:button="@null" android:layout_weight="1"/> </RadioGroup> </RelativeLayout>
效果图如下:
二、用LinearLayout 实现上述效果
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" ></FrameLayout> <RadioGroup android:id="@+id/rg_but" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="@null" android:text 4000 ="1" android:drawableTop="@mipmap/ic_launcher" android:gravity="center_horizontal" android:layout_weight="1"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2" android:drawableTop="@mipmap/ic_launcher" android:gravity="center_horizontal" android:button="@null" android:layout_weight="1"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3" android:drawableTop="@mipmap/ic_launcher" android:gravity="center_horizontal" android:button="@null" android:layout_weight="1"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4" android:drawableTop="@mipmap/ic_launcher" android:gravity="center_horizontal" android:button="@null" android:layout_weight="1"/> </RadioGroup> </LinearLayout>
效果图如下:
上述代码实现的效果图与RelativeLayout效果是一样的,在LinearLayout布局中我们用的是weight属性实现了效果:
android:layout_height="0dp" android:layout_weight="1"
由效果图可知在线性(LinearLayout)布局中,wdight的作用是将屏幕剩余空间按比例来进行分配的。如本题中 android:layout_weight=”1”表示讲除去RadioGroup部分的空间进行分配,1表示剩余的空间全部用FrameLayout来进行填充。这样便会实现上面的效果图
相关文章推荐
- Android线性布局LinearLayout,及盒模型,weight权值,ScrollView和HorizontalScrollView【内容溢出(内容太多,高度/宽度大于能显示的最大高度/宽度)】
- Android:深度探究线性布局LinearLayout中权重(layout_weight)属性
- 线性布局(LinearLayout)下layout_weight用法分析
- android之线性布局LinearLayout以及weight权重使用
- android之线性布局LinearLayout以及weight权重使用
- 线性布局LinearLayout layout_weight属性(转)
- android之线性布局LinearLayout以及weight权重使用
- Android 布局之LinearLayout 子控件weight权重的作用详析(转)
- Android 线性布局(LinearLayout)相关官方文档 - 布局參数部分
- Android UI控件系列:LinearLayout(线性布局)
- [置顶] Android--(2)--详解线性布局(LinearLayout)
- Andriod的相对布局(RelativeLayout)和线性布局(LinearLayout)
- Android 线性布局(LinearLayout)相关官方文档 - 布局参数部分
- Android 布局之LinearLayout 子控件weight权重的作用详析
- Android BGABadgeView:BGABadgeLinearLayout以整体线性布局作为BadgeView(3)
- Android 使用LinearLayout.getChildAt(i)获取一个线性布局的view,并实现content中实现方法
- android布局之LinearLayout, layout_weight
- 对LinearLayout线性布局中Layout_weight的误解
- New UI-布局之LinearLayout(线性布局)详解
- Android线性布局(Linear Layout)