我不明白如何使用这个属性。谁能多告诉我一点吗?
当前回答
http://developer.android.com/guide/topics/ui/layout-objects.html#linearlayout
Layout_weight定义了控件相对于其他控件必须获得的空间大小。
其他回答
除了其他答案之外,最重要的是将布局宽度(或高度)设置为0px
android:layout_width="0px"
否则你会看到垃圾
添加android:autoSizeTextType="uniform"将自动调整文本大小
顾名思义,布局权重指定了特定字段或小部件应该占据屏幕空间的数量或百分比。 如果我们在水平方向上指定权重,那么我们必须指定layout_width = 0px。 类似地,如果我们在垂直方向上指定权重,那么我们必须指定layout_height = 0px。
如果有多个视图跨越一个LinearLayout,那么layout_weight会给每个视图一个比例大小。layout_weight值越大的视图“权重”就越大,因此它得到的空间也就越大。
这里有一张图片让事情更清楚。
理论
布局权重一词与数学中的加权平均概念有关。这就像在大学课堂上,家庭作业占30%,出勤率占10%,期中考试占20%,期末考试占40%。这些部分的分数加在一起,就是你的总成绩。
布局重量也是一样的。水平线性布局中的每个视图可以占据总宽度的一定百分比。(或垂直线性布局高度的百分比。)
布局
你使用的LinearLayout看起来像这样:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- list of subviews -->
</LinearLayout>
注意,对于LinearLayout,你必须使用layout_width="match_parent"。如果您使用wrap_content,那么它将不起作用。还要注意,layout_weight对RelativeLayouts中的视图不起作用(参见这里和这里处理这个问题的SO答案)。
视图
水平LinearLayout中的每个视图看起来像这样:
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
注意,你需要使用layout_width="0dp"和layout_weight="1"。忘记这一点会给许多新用户带来问题。(请参阅本文,了解不将宽度设置为0可以获得的不同结果。)如果你的视图是在垂直线性布局,那么你会使用layout_height="0dp",当然。
在上面的Button示例中,我将权重设置为1,但你可以使用任何数字。重要的是总数。你可以看到在我发布的第一张图片中的三行按钮中,数字都是不同的,但由于比例是相同的,加权宽度在每一行中都没有改变。有些人喜欢使用和为1的十进制数字,这样在复杂的布局中,每个部分的权重就很清楚了。
最后一点。如果你有很多使用layout_weight的嵌套布局,它可能对性能不利。
额外的
下面是顶部图片的xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android:layout_weight="
android:textSize="24sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="2" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android:layout_weight="
android:textSize="24sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="10"
android:text="10" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="20" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="10"
android:text="10" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android:layout_weight="
android:textSize="24sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text=".25" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".50"
android:text=".50" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text=".25" />
</LinearLayout>
</LinearLayout>
请查看LinearLayout的weightSum和每个视图的layout_weight。android:weightSum="4" android:layout_weight="2" android:layout_weight="2"他们的layout_height都是0px,但我不确定这是相关的
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="4">
<fragment android:name="com.example.SettingFragment"
android:id="@+id/settingFragment"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="2"
/>
<Button
android:id="@+id/dummy_button"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="2"
android:text="DUMMY"
/>
</LinearLayout>
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 碎片中的onCreateOptionsMenu
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件