具有两个宽度相等的子级的LinearLayout [英] LinearLayout with two children of equal width
问题描述
我在让LinearLayout
的两个子具有相同的宽度时遇到一些问题.这就是我得到的:
I'm having some issues getting two children of a LinearLayout
to have the same width. This is what I am getting:
这是我用于灰色框的布局xml:
And here is my layout xml for the grey box:
<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
与weightSum
和宽度0dp
一起设置会产生所需的效果(图像的大小与pink'rate用户相同'部分),但事实并非如此.
I was assuming that setting the layout_weight
of the children of the root LinearLayout
along with a weightSum
and width of 0dp
would produce the desired effect (image being the same size as the pink 'rate user' section) however this isn't the case.
我想念什么?
这就是我想要的样子
照片和粉红色的线性布局的宽度应相等.
The photo and the pink linear layout should be equal widths.
推荐答案
android:weightSum="2"
应该在两个子ImageViews的父级上,而不在上级父级上.否则尝试将权重设置为0.5
并查看其是否有效.
android:weightSum="2"
should be on the parent of the two children ImageViews, not on the upper parent. Or else try to set weightsas 0.5
and see if it works.
此外,当使用这样的权重时,两个图像视图的宽度应为android:layout_width="0dp"
.
Also, the widths of the two image views should be android:layout_width="0dp"
when using weights like this.
接下来,按比例放大图像以填充空间.详细信息此处.
Next, scale up your images to fill space. Details here.
这篇关于具有两个宽度相等的子级的LinearLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!