我想为包含一些按钮的LinearLayout定义一个宽度百分比(70%),这样我可以将其居中,子按钮可以填充父按钮.下面是一张图片,展示了我的意思:

示例

我当前的布局如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:id="@+id/layoutContainer" android:orientation="vertical">
    <LinearLayout android:layout_width="fill_parent"
        android:id="@+id/barContainer" android:orientation="horizontal"
        android:layout_height="40dp" android:background="@drawable/titlebackground">
        <ImageView android:id="@+id/barLogo" android:src="@drawable/titlelogo"
            android:layout_gravity="center_vertical" android:adjustViewBounds="true"
            android:layout_height="25dp" android:layout_width="wrap_content"
            android:scaleType="fitXY" android:paddingLeft="5dp"></ImageView>
    </LinearLayout>
    <TextView android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:gravity="center_horizontal"
        android:id="@+id/searchTip" android:text="@string/searchTip"
        android:paddingTop="10dp" android:paddingBottom="10dp"></TextView>
    <LinearLayout android:layout_height="wrap_content"
        android:id="@+id/linearLayout1" android:orientation="vertical" android:layout_width="wrap_content">
        <Button android:text="Button" android:id="@+id/button1"
            android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:layout_width="wrap_content" android:id="@+id/button2" android:layout_height="wrap_content" android:text="Button"></Button>
        <Button android:layout_width="wrap_content" android:id="@+id/button3" android:layout_height="wrap_content" android:text="Button"></Button>
    </LinearLayout>
</LinearLayout>

我指的LinearLayout的id为:linearLayout1.我该怎么做?

推荐答案

必须设置元素的权重属性.作为子元素,为你的线性布局创建三个RelativeLayouts,并设置权重0.15、0.70、0.15.然后将按钮添加到第二个RelativeLayout(重量为0.70的按钮).

这样地:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:id="@+id/layoutContainer" android:orientation="horizontal">
    <RelativeLayout
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="0.15">
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="0.7">
        
        <!-- This is the part that's 70% of the total width. I'm inserting a LinearLayout and buttons.-->   
            <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="vertical">
                
                <Button 
                    android:text="Button1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content">
                </Button>
                <Button
                    android:text="Button2"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content">
                </Button>
                <Button
                    android:text="Button3"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content">
                </Button>
                
            </LinearLayout>
        <!-- 70% Width End-->
        
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="0.15">
    </RelativeLayout>
</LinearLayout>

为什么权重是0.15、0.7和0.15?因为总重量是1,0.7是总重量的70%.

Result:

enter image description here

编辑:感谢@SimonDeveloper指出方向应该是水平的,而不是垂直的,@Andrew指出权重可以是小数而不是整数.

Android相关问答推荐

Okhttp3请求.Builder的Post函数不会接受FormBody作为参数

编写Inbox需要具有匹配兼容性的Kotlin版本

理解修饰符<;方法>;与修饰符<;方法>;:效果和行为解释(Android开发者Jetpack Compose)

如何在安卓系统上使用Float16霓虹灯?

这款应用与最新版本的Android不兼容.在Android 14中

Android 12+BLE字节不同

Jetpack Compose X.dp 性能问题?

错误:参数的类型必须是用@Entity注释的类或其集合/array. java.lang.String tocd);

Android 构建失败:找不到 flexbox2.0.1.aar

如何在Android Studio中禁用文件中的Github用户名引用?

Jetpack Compose 中的用户在线指示器

如何在 Android Studio 中为带有 Room 的 SQLite 编写需要参数的查询?

在 compose 中做可变状态堆栈

列语义在列中的第一个元素之后读取

Kotlin 协程、 retrofit 、android

如何用jetpack compose实现垂直李克特量表

Android 12 通过包管理器中断 APK 安装?

Jetpack Compose:mutableStateOf 不随流量更新

对话框中的内容不可见

新安装 Expo Go 的 android 设备上的 sdk 46.0.0 (@snack/sdk.46.0.0) 出错