线性布局LinearLayout是最常用的布局之一,它可以把包含的子元素(View)排列成一列或者一行,即垂直方向或者水平方向,默认是水平方向,方向可以通过setOrientation()方法设置,可以通过setGravity设置子元素的对齐方式,还可以通过子元素的weight属性设置子元素在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">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="red"
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="green"
android:gravity="center_horizontal"
android:background="#00aa00"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="blue"
android:gravity="center_horizontal"
android:background="#0000aa"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="yellow"
android:gravity="center_horizontal"
android:background="#aaaa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="row one"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="row two"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="row three"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="row four"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
子元素的layout_weight 属性,值越小,所占得比重越大,分为两种情况:
举例说明:
如果水平显示,子元素的layout_width属性值为fill_parent,则layout_weight属性值越小,占得显示比例越大,layout_width属性值为wrap_content,则layout_weight属性值越小,显示比例越小。
如果是垂直显示,则注意子元素的layout_height属性,情况和水平一样
分享到:
相关推荐
LinearLayout是线性布局控件:要么横向排布,要么竖向排布 常用属性: android:gravity————设置的是控件自身上面的内容位置 android:layout_gravity—–设置控件本身相对于父控件的显示位置 android:layout_...
用Android Studio 2.3.3做的线性布局例子,主要用到的标签有:<LinearLayout></LinearLayout>;主要用到的设置有:android:orientation="horizontal"、android:layout_weight="1"。
<LinearLayout nandroid:layout_width=“0dp” android:layout_weight u201c1” android:layout_height=“match_parent” android:background> 运行效果如下 因为灰色部分和黑色部分权重相同故两部分的宽度各占总...
android:layout_weight="1" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:layout_marginRight="10dp...
最常见的解决方案是用线性布局,自适应当然是按比例,但布局中无%的概念,那就要用到layout_weight了,该属性的作用是决定控件在其父布局中的显示权重(具体概念就不多说了)。 这里用一个LinearLayout 将数字...
这是一个Android学生项目,使用常见的UI元素,例如-Button -TextView -Relative Layout -Linear Layout -DisplayMetrics...按钮的宽度为零,其整齐的布局通过将其layout_weight =“ 1”(LinearLayout)放置来进行处理
LinearLayout(线性布局)提供了控件水平垂直排列的模型,同时可以通过设置子控件的weight布局参数控制各个控件在布局中的相对大小。水平(vertical)垂直(horizontal)fill-parent:占满整个屏幕,wrap-content:刚好适合...