Android:weight,margin,padding详解实例
![标题图](http://upload-images.jianshu.io/upload_images/11158618-d94ba57d9e035eee.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
weight详解
weight
是用来等比例划分区域的属性。
案例代码
![dashucoding](https://upload-images.jianshu.io/upload_images/11158618-007bdd3a422e7a9f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#ADFF2F"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#DA70D6"
android:layout_weight="2"/>
</LinearLayout>
![dashucoding](https://upload-images.jianshu.io/upload_images/11158618-e9ae71b3c25fa1df.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#ADFF2F"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#DA70D6"
android:layout_weight="1"/>
</LinearLayout>
![dashucoding](https://upload-images.jianshu.io/upload_images/11158618-67db251586e59e49.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="one"
android:background="#98FB98"
/>
<TextView
android:layout_weight="2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="two"
android:background="#FFFF00"
/>
<TextView
android:layout_weight="3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="three"
android:background="#FF00FF"
/>
</LinearLayout>
![dashucoding](https://upload-images.jianshu.io/upload_images/11158618-920a7343d5d0ae74.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="one"
android:background="#98FB98"
/>
<TextView
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="two"
android:background="#FFFF00"
/>
<TextView
android:layout_weight="3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="three"
android:background="#FF00FF"
/>
</LinearLayout>
为什么出现2:1的案例呢?three怎么就不见了呢?
- 每个宽度为
match_parent
,屏幕为1,那么屏幕就是1-3=-2个math_parent
- 计算方法,
1 - 2 * (1/6) = 2/3,1 - 2 * (2/6) = 1/3, 1 - 2 * (3/6) = 0
,结果是one占了两份,two占了一份,three什么都没有
margin
,padding
详解
margin
代表是偏移,表示组件离容器偏移量,margin
针对的是容器中的组件元素;padding
代表是填充,填充的对象针对的是组件中的元素。
<RelativeLayout
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">
<Button
android:id="@+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Button"/>
<Button
android:paddingLeft="100dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Button"
android:layout_toRightOf="@id/btn1"/>
<Button
android:id="@+id/btn2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Button"
android:layout_alignParentBottom="true"/>
<Button
android:layout_marginLeft="100dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Button"
android:layout_toRightOf="@id/btn2"/>
</RelativeLayout>
结语
- 本文主要讲解 Android:weight,margin,padding详解实例
- 下面我将继续对其他知识 深入讲解 ,有兴趣可以继续关注
- 小礼物走一走 or 点赞
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步