android - 具有两个等宽子项的 LinearLayout

标签 android android-layout

我在让 LinearLayout 的两个 child 具有相同宽度时遇到了一些问题。这就是我得到的:

screenshot

这是我的灰色框布局 xml:

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2"
    android:background="@color/medium_grey"
    >

    <ImageView 
        android:id="@+id/profile_photo"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:src="@drawable/placeholder_profile_photo"
        android:scaleType="fitCenter"
        android:contentDescription="@string/blank"
        android:layout_weight="1"
        />

    <LinearLayout 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_weight="1"
        android:layout_gravity="center_vertical"
        android:background="@color/alert"
        >

        <TextView
            android:id="@+id/profile_rate_header"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Rate User"
            />

        <LinearLayout 
            android:id="@+id/profile_action_rate_user"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_weight="1"
            android:gravity="center"
            >

            <ImageView 
                android:id="@+id/profile_action_rate_up"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/user_rate_up"
                />

            <ImageView 
                android:id="@+id/profile_action_rate_down"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/user_rate_down"
                />

        </LinearLayout>
    </LinearLayout>
</LinearLayout>

我假设设置根 LinearLayout 子项的 layout_weight 以及 weightSum0dp< 的宽度 会产生预期的效果(图像与粉红色的“评价用户”部分大小相同),但事实并非如此。

我错过了什么?

编辑:

这是我想要的样子

enter image description here

照片和粉红色的线性布局应该等宽。

最佳答案

android:weightSum="2" 应该在两个子 ImageView 的父级上,而不是在上级父级上。或者尝试将权重设置为 0.5 并查看它是否有效。

此外,当使用这样的权重时,两个 ImageView 的宽度应为 android:layout_width="0dp"

接下来,放大图像以填充空间。详情 here .

关于android - 具有两个等宽子项的 LinearLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17531033/

相关文章:

java - 在android studio中创建新项目时出现junit错误

android - 是否可以将多个组件绑定(bind)到android中的intentservice

android - 渲染问题注意 : One or more layouts are missing the layout_width or layout_height attributes. 这些在大多数布局中都是必需的

android - 输入时清除 EditText 中的文本

android - 如何在一组特定 fragment 之间共享 ViewModel 范围,而不使用 NavGraphs 或将其范围限定到 Activity ?

android - 我正在使用 Kotlin 开发一个 Android 应用程序,我正在尝试从 Firestore 获取文档引用

Android 阻止键盘关闭

android - ListView 中 ListItem 的内容垂直居中

android - java.lang.IllegalArgumentException : width and height must be &gt; 0 异常

android - 将 float 操作按钮集成到带有工具栏的线性布局中