保持RelativeLayout高度尽可能小,但增加子项高度以填充所有可用空间
我的RelativeLayout 包含几个不同高度的ImageView/TextView 控件,例如20、70、35。这些控件排列在一行中。我希望它们将高度扩展到与最大控件高度相同的值,例如 70。问题是,在我的布局中,RelativeLayout 容器和所有控件都扩展到屏幕高度。
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:src="@drawable/image1"
android:background="#FFCCDDEE"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
/>
<ImageView
android:src="@drawable/image2"
android:background="#FFCCDDEE"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_centerInParent="true"
/>
<ImageView
android:src="@drawable/image3"
android:background="#FFCCDDEE"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
我可以将relativelayout高度设置为70,但考虑到我事先不知道子控件的大小。
我用LinearLayout也能达到想要的效果,但想用RelativeLayout。
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:src="@drawable/image1"
android:background="#FFCCDDEE"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
/>
<ImageView
android:src="@drawable/image2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
/>
<ImageView
android:src="@drawable/image3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
/>
</LinearLayout>
有什么想法吗?
My RelativeLayout contains few ImageView/TextView controls of different height, e.g. 20, 70, 35. The controls alligned in a row. I want them to expand their height to the same value of the maximum control height, e.g. 70. The problem is that with my layout the RelativeLayout container and all controls expanded to the screen height instead.
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:src="@drawable/image1"
android:background="#FFCCDDEE"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
/>
<ImageView
android:src="@drawable/image2"
android:background="#FFCCDDEE"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_centerInParent="true"
/>
<ImageView
android:src="@drawable/image3"
android:background="#FFCCDDEE"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
I can set RelativeLayout height to 70, but consider that I don't know the child controls size upfront.
I also can achieve desired effect with LinearLayout, but want to use the RelativeLayout.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:src="@drawable/image1"
android:background="#FFCCDDEE"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
/>
<ImageView
android:src="@drawable/image2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
/>
<ImageView
android:src="@drawable/image3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
/>
</LinearLayout>
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否考虑过使用填充/边距?
该 XML 应该生成 140 x 140 的图像(我认为)。它不会是动态的,但 XML 无法执行此类动态操作。
如果您想做一些更动态的事情,您可以编写代码来拉取所有元素的大小,找到最大的,然后将所有对象设置为该大小。
这个链接可能对此有用。
祝你好运!
Have you thought about using padding / margins?
That XML should produce an image that is 140 x 140 (I think). It won't be dynamic, but the XML isn't able to do dynamic things like that.
If you want to do something that is more dynamic you could write code that would pull all your elements size's, find the largest, then set all of your objects to that size.
This link might be useful for that.
Good luck!