Layout_weight 以任意方式划分
Layout_weight divides in arbitrary fashion
android:layout_weight beginner's question
和
这两个链接 post 一个类似的问题,但这些解决方案对我没有帮助。
xml代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
tools:context="com.example.pranavhd.assign1.MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="horizontal"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="horizontal"></LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
因此,如果我将总重量设为 10,并将其按 7:3 的比例进行划分,就会得到这种不均匀的布局。可以看出,weight为7的看起来比3的小。我也试过设置layout_height = 0px或layout_width = 0px。在这种情况下,布局缩小为 0。Where weight ratio are 7:3
With layout_height = 0
步骤 1
新建一个项目,打开xml布局文件,拖拽一个LinearLayout(水平),在线性布局中添加两个按钮。
您的布局文件现在应该如右图所示。
注意按钮是如何向左聚集的,这是权重可以发挥作用的地方。
步骤 2
现在切换到文本视图并将 LinearLayout(水平)的 weightSum 设置为 100。
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:weightSum="100">
步骤 3
现在给每个按钮添加一个layout_weight属性,使值为50。
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_weight="50"/>
步骤 4
现在,如果您再次查看显示屏,您的按钮应该 space均匀分布。
如果 weightSum 被认为是可用 space 的 100%,并且 layout_weight 是元素将占该 100% 的百分比,那么思考它如何工作的一种方式.
由于我们给每个按钮赋予了 50 的权重,因此两者将占据 space 的 50%。
我们也可以将 weightSum 的值设置为 1,并将按钮的 layout_weight 设置为 0.5 以获得相同的效果。
希望对你有用:)
要获得 layout_weight
效果,您必须让 layout_height
与 wrap_content
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="horizontal"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="horizontal"></LinearLayout>
android:layout_weight beginner's question
和
这两个链接 post 一个类似的问题,但这些解决方案对我没有帮助。
xml代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
tools:context="com.example.pranavhd.assign1.MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="horizontal"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="horizontal"></LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
因此,如果我将总重量设为 10,并将其按 7:3 的比例进行划分,就会得到这种不均匀的布局。可以看出,weight为7的看起来比3的小。我也试过设置layout_height = 0px或layout_width = 0px。在这种情况下,布局缩小为 0。Where weight ratio are 7:3
With layout_height = 0
步骤 1
新建一个项目,打开xml布局文件,拖拽一个LinearLayout(水平),在线性布局中添加两个按钮。
您的布局文件现在应该如右图所示。
注意按钮是如何向左聚集的,这是权重可以发挥作用的地方。
步骤 2
现在切换到文本视图并将 LinearLayout(水平)的 weightSum 设置为 100。
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:weightSum="100">
步骤 3
现在给每个按钮添加一个layout_weight属性,使值为50。
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_weight="50"/>
步骤 4
现在,如果您再次查看显示屏,您的按钮应该 space均匀分布。
如果 weightSum 被认为是可用 space 的 100%,并且 layout_weight 是元素将占该 100% 的百分比,那么思考它如何工作的一种方式.
由于我们给每个按钮赋予了 50 的权重,因此两者将占据 space 的 50%。
我们也可以将 weightSum 的值设置为 1,并将按钮的 layout_weight 设置为 0.5 以获得相同的效果。
希望对你有用:)
要获得 layout_weight
效果,您必须让 layout_height
与 wrap_content
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="horizontal"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="horizontal"></LinearLayout>