Android 将 layout_weight 设置为垂直 LinearLayout 中的宽度
Android set layout_weight to width in vertical LinearLayout
我有一个垂直的LinearLayout,里面有一个按钮。我想让这个按钮成为 LinearLayout 宽度的 70% 并将其居中。
我试过将layout_weight
设置为0.7,同时将layout_width
设置为0,但由于它是垂直的LinearLayout,它不会影响宽度,只会影响高度。
如何更改垂直布局中的宽度权重?我是否必须为按钮添加一个嵌套的水平 LinearLayout 以便我可以设置它的权重?
linearLayout -> orientation = horizontal
button -> layout width = 0dp
-> layout height = wrap_content
-> layout weight = 1
尝试使用新的 PercentRelativeLayout,您可以在没有嵌套布局的情况下实现您正在尝试的效果。
将其包含在您的依赖项中:com.android.support:percent:25.1.0
然后在您的代码中
<android.support.percent.PercentRelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
<Button
app:layout_widthPercent="70%"
android:layout_height="wrap_content"/>
...
</android.support.percent.PercentRelativeLayout>
请检查一下。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.7"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/yourRequiredButton"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/container"
android:layout_weight="0.3"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
试试这个:
<LinearLayout>
<Space android:layout_weight=0.15/>
<Button android:layout_weight=0.7/>
<Space android:layout_weight=0.15/>
</LinearLayout>
https://developer.android.com/reference/android/widget/Space.html
我还建议您将这些值放在 dimens.xml
文件中。
我有一个垂直的LinearLayout,里面有一个按钮。我想让这个按钮成为 LinearLayout 宽度的 70% 并将其居中。
我试过将layout_weight
设置为0.7,同时将layout_width
设置为0,但由于它是垂直的LinearLayout,它不会影响宽度,只会影响高度。
如何更改垂直布局中的宽度权重?我是否必须为按钮添加一个嵌套的水平 LinearLayout 以便我可以设置它的权重?
linearLayout -> orientation = horizontal
button -> layout width = 0dp
-> layout height = wrap_content
-> layout weight = 1
尝试使用新的 PercentRelativeLayout,您可以在没有嵌套布局的情况下实现您正在尝试的效果。
将其包含在您的依赖项中:com.android.support:percent:25.1.0
然后在您的代码中
<android.support.percent.PercentRelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
<Button
app:layout_widthPercent="70%"
android:layout_height="wrap_content"/>
...
</android.support.percent.PercentRelativeLayout>
请检查一下。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.7"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/yourRequiredButton"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/container"
android:layout_weight="0.3"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
试试这个:
<LinearLayout>
<Space android:layout_weight=0.15/>
<Button android:layout_weight=0.7/>
<Space android:layout_weight=0.15/>
</LinearLayout>
https://developer.android.com/reference/android/widget/Space.html
我还建议您将这些值放在 dimens.xml
文件中。