前言
layout_weight按自己的理解就是按比例分配LinearLayout的子控件的大小,为了了解这个属性,接下来就边写代码,边看效果图来理解该属性。
正文
首先将根布局修改为LinearLayout,将orientation属性设置为horizontal,使LinearLayout的子控件水平排列,再给LinearLayout放三个TextView作为子布局,内容分别显示1,2,3;TextView的宽度设置为0dp,layout_weight的属性都填写为1,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#00ff88"
android:layout_weight="1"
android:gravity="center"
android:text="1" />
<TextView
android:layout_width="0dp"
android:layout_h