weight属性可以理解为权重。
关于weight属性有一个计算公式,但是并不需要记住那个公式,所以我也没记住=。=

我们只需要记住标准用法就好啦

1、用weight设置宽:将控件的width设置为0dp
android:layout_width=”0dp”
android:layout_weight=”2”
2、用weight设置高:将控件的height设置为0dp
android:layout_height=”0dp”
android:layout_weight=”2”

示例:

设置button1宽占1/3,button2占2/3

    <LinearLayout  android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" >        <Button  android:id="@+id/button1" android:layout_height="wrap_content" <!-- 注意这两行 -->            android:layout_width="0dp"            android:layout_weight="2"            android:text="111111" />        <Button  android:id="@+id/button2" <!-- 注意这两行 -->            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="wrap_content"            android:text="222222" />    </LinearLayout>

效果图:

根据上例:
button1 weight=2
button2 weight=1

所以button1的占比为: 2/(1+2)=2/3;

示例2:

 <LinearLayout  android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >        <Button  android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="3" android:text="111111" />        <Button  android:id="@+id/button4" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:text="2222222" />    </LinearLayout>

效果图:

根据上例:
button1 weight=3
button2 weight=1

所以button1的占比为: 3/(1+3)=3/4;

更多相关文章

  1. Android控件之文本控件---TextView 两种效果+SpannableString
  2. Android(安卓)ApiDemos示例解析(128):Views->Layout Animation->
  3. 设置自定义ProgressBar样式
  4. android窗口动画和过渡动画(activity和dialog)
  5. Android在开发中的实用技巧之DialogFragment和AlertDialog(v7包)
  6. Android(安卓)Studio中aidl的使用示例
  7. Android之RecyclerView简单使用(三)
  8. Android仿饿了么搜索功能
  9. Android抓包工具使用与错误

随机推荐

  1. Android(安卓)layout文件中 '?' 的作用
  2. Android中shape的使用
  3. android - 为安全而设计 - 1 - 开发文档
  4. Android中自己定义一个shade.xml
  5. Android---网络编程之OkHttp3整体结构了
  6. android与ios之争之我见
  7. 初涉Android之文件保存
  8. Android随笔之——Android单元测试
  9. Android中RelativeLayout各个属性的含义
  10. Android查看外部依赖jar的源码'Android(