您的位置:首页 > 移动开发 > Android开发

Android开发总结笔记 LinearLayout(线性布局) 1-1-1

2015-09-21 16:05 501 查看
LinearLayout的继承结构





事实上所有的布局类都会继承ViewGroup这个类。从字面上理解,ViewGroup就是一组View的意思。(LinearlayoutAPI)



下面来看一下LinearLayout在xml中的一些属性吧



XML属性对应JAVA方法
android:orientationsetOrientation(int)
android:weightNum
android:dividersetDividerDrawable(Drawable)
android:gravitysetGravity(int)
android:baseLineAlignedsetBaseLineAligned(boolean)
android:baseLineAlignedChildIndexsetBaseLineAlignedChildInxex(int)
android:measureWithLargestChildsetMeasureWithLargestChildEnable(boolean)
android:orientation两个值:Vertical垂直Horizontal水平
android:divider参数必须是一个shape或者一张图片、结合showDividers和dividerPadding属性使用,要设置宽高才能显示出来,如果android:shape使用line,只能用stroke来显示颜色

子控件中的android:weight属性表示该组件在linearLayout中所占的权重。

shape.xml

<?xmlversion="1.0"encoding="utf-8"?>
<shapexmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="5dp"
android:height="5dp"></size>
<solidandroid:color="@android:color/black"></solid>
</shape>


activity_main.xml

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="true"
android:divider="@drawable/shape"
android:orientation="vertical"
android:showDividers="middle">
<TextView
android:layout_width="100dp"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/holo_blue_bright"
android:text="android"
android:textSize="20sp"/>
<TextView
android:layout_width="100dp"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/holo_green_dark"
android:gravity="center_vertical"
android:text="Iloveit"
android:textSize="20sp"/>
</LinearLayout>


效果图:





android:baseLineAligned基准线的概念:









红色那条就是基准线

设置前



设置后





android:baseLineAlignedChildIndex这个属性也是一样的原理

android:measureWithLargestChild少用。直接忽略了。

重要提示

当android:orientation="vertical"时,只有水平方向的设置才起作用,垂直方向的设置不起作用。

即:left,right,center_horizontal是生效的。

当android:orientation="horizontal"时,只有垂直方向的设置才起作用,水平方向的设置不起作用。

即:top,bottom,center_vertical是生效的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: