线性布局(LinearLayout)

一、权重详解

用法归纳: 按比例划分水平方向:将涉及到的View的android:width属性设置为0dp,然后设置为android weight属性设置比例即可;
类推,竖直方向,只需设android:height为0dp,然后设weight属性即可!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.layout.MainActivity">
    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:background="#FFFF00"
        android:text="One" />
    <TextView
        android:layout_width="0dp"
        android:layout_weight="2"
        android:layout_height="match_parent"
        android:background="#FF00FF"
        android:text="Two" />
</LinearLayout>

二、设置分割线

方法1、利用控件间隔和背景

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="#000000"
    tools:context="com.example.layout.MainActivity">
    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:background="#FFFF00"
        android:text="One"
        android:layout_marginRight="1dp"/>
    <TextView
        android:layout_width="0dp"
        android:layout_weight="2"
        android:layout_height="match_parent"
        android:background="#FF00FF"
        android:text="Two" />
</LinearLayout>


方法2、添加View

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.layout.MainActivity">
    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:background="#FFFF00"
        android:text="One"
        />
    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="#000000"/>
    <TextView
        android:layout_width="0dp"
        android:layout_weight="2"
        android:layout_height="match_parent"
        android:background="#FF00FF"
        android:text="Two" />
</LinearLayout>


3、第二种则是使用LinearLayout的一个divider属性,直接为LinearLayout设置分割线 这里就需要你自己准备一张线的图片了

 1)android:divider设置作为分割线的图片 

2)android:showDividers设置分割线的位置

,none(无),begining(开始),end(结束),middle(每两个组件间) 

3)dividerPadding设置分割线的Padding

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:divider="@drawable/line"
    android:showDividers="middle"
    android:dividerPadding="0dp"
    tools:context="com.example.layout.MainActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:text="One"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_weight="2"
        android:layout_height="0dp"
        android:text="Two" />
</LinearLayout>

三、Layout_gravity的注意事项

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值