我不明白如何使用这个属性。谁能多告诉我一点吗?
当前回答
添加android:autoSizeTextType="uniform"将自动调整文本大小
其他回答
layout_weight tells Android how to distribute your Views in a LinearLayout. Android then first calculates the total proportion required for all Views that have a weight specified and places each View according to what fraction of the screen it has specified it needs. In the following example, Android sees that the TextViews have a layout_weight of 0 (this is the default) and the EditTexts have a layout_weight of 2 each, while the Button has a weight of 1. So Android allocates 'just enough' space to display tvUsername and tvPassword and then divides the remainder of the screen width into 5 equal parts, two of which are allocated to etUsername, two to etPassword and the last part to bLogin:
<LinearLayout android:orientation="horizontal" ...>
<TextView android:id="@+id/tvUsername"
android:text="Username"
android:layout_width="wrap_content" ... />
<EditText android:id="@+id/etUsername"
android:layout_width="0dp"
android:layout_weight="2" ... />
<TextView android:id="@+id/tvPassword"
android:text="Password"
android:layout_width="wrap_content" />
<EditText android:id="@+id/etPassword"
android:layout_width="0dp"
android:layout_weight="2" ... />
<Button android:id="@+id/bLogin"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Login"... />
</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>
对我来说最好的解释之一是这个(来自Android教程,请参阅第7步):
layout_weight is used in LinearLayouts to assign "importance" to Views within the layout. All Views have a default layout_weight of zero, meaning they take up only as much room on the screen as they need to be displayed. Assigning a value higher than zero will split up the rest of the available space in the parent View, according to the value of each View's layout_weight and its ratio to the overall layout_weight specified in the current layout for this and other View elements. To give an example: let's say we have a text label and two text edit elements in a horizontal row. The label has no layout_weight specified, so it takes up the minimum space required to render. If the layout_weight of each of the two text edit elements is set to 1, the remaining width in the parent layout will be split equally between them (because we claim they are equally important). If the first one has a layout_weight of 1 and the second has a layout_weight of 2, then one third of the remaining space will be given to the first, and two thirds to the second (because we claim the second one is more important).
顾名思义,布局权重指定了特定字段或小部件应该占据屏幕空间的数量或百分比。 如果我们在水平方向上指定权重,那么我们必须指定layout_width = 0px。 类似地,如果我们在垂直方向上指定权重,那么我们必须指定layout_height = 0px。
简而言之,layout_weight指定了布局中要分配给View的额外空间的多少。
线性布局支持为各个子节点分配权重。此属性为视图分配了一个“重要性”值,并允许它展开以填充父视图中的任何剩余空间。视图的默认权重为0。
计算子节点之间的剩余空间
一般情况下,公式为:
分配给孩子的空间=(孩子的个人体重)/(线性布局中每个孩子的体重之和)
示例1
如果有三个文本框,其中两个声明权重为1,而第三个文本框没有权重(0),那么剩余空间的分配如下:
第一个文本框= 1/(1+1+0) 第二个文本框= 1/(1+1+0) 第三个文本框= 0/(1+1+0)
示例2
假设在水平行中有一个文本标签和两个文本编辑元素。标签没有指定layout_weight,因此它占用呈现所需的最小空间。如果这两个文本编辑元素的layout_weight都设置为1,父布局中的剩余宽度将在它们之间平均分配(因为我们声称它们同样重要)。
计算:
第一个标签= 0/(0+1+1) 第二个文本框= 1/(0+1+1) 第三个文本框= 1/(0+1+1)
如果第一个文本框的layout_weight为1,第二个文本框的layout_weight为2,那么剩余空间的三分之一将给第一个文本框,三分之二给第二个文本框(因为我们声称第二个文本框更重要)。
计算:
第一个标签= 0/(0+1+2) 第二个文本框= 1/(0+1+2) 第三个文本框= 2/(0+1+2)
文章来源
推荐文章
- 警告: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文件