1. Layout_weight.
LinearLayout还可以为其包含控件指定填充权值layout_weight(简称weight)。 这样就允许其包含的控件可以填充屏幕上的剩余空间。这也避免了所有控件挤成一堆的情况,而是允许他们放大填充所有空白。剩余的空间会按这些控件指定的权值比例分配屏幕。
默认情况下,weight的值是0,表示按照控件的实际大小显示;如果weight设置高于零。
剩余空间会按照该控件的weight值占所有控件weight的比例分配给该控件。 比如有两个控件,一个weight为1,另外一个是2. 侧剩余空间会把1/(1+2)的部分给控件一,另外2/(1+2)的分配给控件二。也就是权值越大,重要度越大。
如果LinearLayout包含子LinearLayout,子LinearLayout之间的权值越大的,重要度则越小。如果有LinearLayout A包含LinearLayout C,D,C的权值为2,D的权值为1,则屏幕的2/3空间分给权值为1的D,1/3分给权值为2的C。在LinearLayout嵌套的情况下,子LinearLayout必须要设置权值,否则默认的情况是未设置权值的子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:id="@+id/linelayout1"
android:layout_width = "fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="2"
>
<TextView
android:text="block with weight 2 is smaller than the block with weight 1"
android:gravity="center_horizontal"
android:textSize="8pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:id ="@+id/lineLayout1"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
android:layout_weight="1"
/>
<TextView
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"
android:layout_weight="2"
/>
</LinearLayout>
</LinearLayout>
效果如下:
2. 其他属性
android:baselineAligned:是否允许用户调整它内容的基线。
android:baselineAlignedChildIndex:当一个线性布局与另一个布局是按基线对齐的一部分,它可以指定其内容的基线对齐方式。
android:gravity:指定如何在该对象中放置此对象的内容(x/y坐标值)。
- 大小: 13.2 KB
分享到:
相关推荐
android:id="@id/Widget_2X4_frame" android:layout_width="fill_parent" android:layout_height="228.0dip" android:layout_marginTop="10.0dip" xmlns:android=...
Android开源代码,自定义Layout,继承自LinearLayout,内部控件自动换行显示。例如:可用来开发数量变化的属性标签群。
www.mars-droid.com/Android开发视频教程 LinearLayout代码 源码 mars老师讲课 android 视频源码 Layout_01(在此特别感谢mars的无私奉献,此代码为跟随视频边学边做的)
Android中使用LinearLayout完成的四书五经首界面的代码清单 .pdf 学习资料 复习资料 教学资源
www.mars-droid.com/Android开发视频教程 LinearLayout TableLayout代码 源码 mars老师讲课 android 视频源码 Layout_04(在此特别感谢mars的无私奉献,此代码为跟随视频边学边做的)
Android Layout多种使用方法源码演示,这个例子中演示了如何使用FrameLayout、如何使用RelativeLayout、如何使用RelativeLayout和LinearLayout以及如何使用TableLayout的例子,都挺实用,平时用的比较多,但是比较...
Android开发学习23】界面布局之线性布局LinearLayout代码
-<LinearLayout android:background="@drawable/aaa" android:weightSum="1" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android=...
LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:tools=”http://schemas.android.com/tools” android:id=”@+id/LinearLayout” android:layout_width=”fill_parent” android...
在做android UI布局时,用了LinearLayout嵌套,发现效果并不如我预料一般 查了下资料,说是要设置layout_weight属性 资料说得不是很清楚,也没仔细看,就去弄,结果越弄越混乱。 于是静下心来,自己写xml测试,发现...
LinearLayout是线性布局控件:要么横向排布,要么竖向排布 常用属性: android:gravity————设置的是控件自身上面的内容位置 android:layout_gravity—–设置控件本身相对于父控件的显示位置 android:layout_...
-<LinearLayout android:background="@drawable/aaa" android:weightSum="1" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android=...
代码如下:<...LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”fill_p
NULL 博文链接:https://hanllove001.iteye.com/blog/1185205
首先说的是LinearLayout布局下的居中一般是这样的: ...<LinearLayout android:layout_width=fill_parent android:layout_height=fill_parent android:gravity=center|center_horizontal|center_vertical> //
<LinearLayout android:layout_width="fill_parent" android:gravity="center_horizontal" android:id="@+id/buttons" android:layout_below="@id/msg" android:padding="5dip" android:layout_height="wrap_...
自定义自动换行LinearLayout LayoutParams使用android.widget.LinearLayout.LayoutParams
本文实例介绍了Android中LinearLayout、AbsoluteLayout的用法,希望能对于初学Android的朋友起到一点帮助作用。具体内容如下: Android 的UI 布局都以Layout 作为容器,并且在上面按照规定排列控件,这方面跟JAVA 的...
2 3 2 android:padding属性和android:layout margin属性 2 3 3 android:layout gravity属性和android:gravity属性 2 4 高级布局技术 第3章 组件 3 1 组件的属性 3 2 文本组件 3 3 按钮组件 3 4 图像组件 3 5 进度...
LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列(通过android:orientation属性来控制),按照相对位置来排列所有的widgets或者其他的containers,超过边界时,某些控件将缺失或消失 2. 线性...