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

来源:互联网 发布:linux echo 打印变量 编辑:程序博客网 时间:2024/06/06 00:53
LinearLayout的继承结构


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


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


XML属性对应JAVA方法android:orientation setOrientation(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
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
 
<size
android:width="5dp"
android:height="5dp"></size>
 
<solid android:color="@android:color/black"></solid>
</shape>
activity_main.xml
<LinearLayout xmlns: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="I love it"
android:textSize="20sp" />
 
 
</LinearLayout>

效果图:

        

  • android:baseLineAligned基准线的概念:
           

            

            红色那条就是基准线

设置前
           
                 


设置后
  • android:baseLineAlignedChildIndex    这个属性也是一样的原理
  • android:measureWithLargestChild   少用。直接忽略了。
重要提示
当 android:orientation="vertical" 时, 只有水平方向的设置才起作用,垂直方向的设置不起作用。
即:left,right,center_horizontal 是生效的。 

当 android:orientation="horizontal" 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用。 
即:top,bottom,center_vertical 是生效的


 

0 0
原创粉丝点击