`
xitongyunwei
  • 浏览: 927008 次
文章分类
社区版块
存档分类
最新评论

A05_Layout_weight的属性设置

 
阅读更多

一、

Layout_weight:weigth的意思是“权值”

这个属性可以设置控件在父布局中的所占比重。

二、

特别注意:Layout_weight的使用前提是父布局还有空余空间。分配父布局剩余的空间,而不是父布局的所有空间。这一点可能在学习的过程中会被一些资料误导。见图中控件One和控件Two

1、假设有两个控件,分别设置它们的Layout_weight值为1,则这两个控件会平分父布局剩余空间。而不是平分父布局的所有空间。见图中控件One和控件Two

2、假设分别设置控件一的Layout_weight的值为1,控件二的Layout_weight的值为2,那么父布局剩余的空间控件一占一份,控件二占两份。显示的效果是控件一的宽和高加上所得的一份空间的宽和高

3、如果想要设置控件一和控件二在父布局中宽的比例是1:2。

方法:将控件一和空间二的宽设置为0dp,设置控件一的Layout_weight的值为1,控件二的Layout_weight的值为2。就可以实现。其它多个控件的比例实现也是类似的。


XMl代码:

<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"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="One的宽是0dp权值为1,Two的宽是200dp权值为1" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff00ff"
        android:orientation="horizontal" >

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="One" />

        <Button
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Two" />
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Three的宽是0dp权值为1,Four的宽是0dp权值为2" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00ffff"
        android:orientation="horizontal" >

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Three" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="Four" />
    </LinearLayout>

</LinearLayout>


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics