Android 布局匹配对等点
我正在寻找对布局系统的一些见解。有谁知道 Android 布局系统中的一种方法,允许 peer View
元素(意味着在同一容器中分组在一起的视图)匹配其高度和宽度属性,而无需创建间隙容器只是为了控制它们的大小?
例如:布局有两个 TextView
元素。第一个应始终将宽度换行到其文本内容,但第二个应与第一个的宽度匹配。可行的解决方案是这样的:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textTwo"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
但是,我必须为两个视图创建一个特殊的容器,以确保它们都与第一个视图中的任何文本的宽度相匹配;给层次结构增加不必要的复杂性。我想知道的是,是否有一个布局属性可以用来执行以下操作:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textTwo"
android:layout_widthMatchPeer="@id/textOne" <!-- An attribute like this -->
android:layout_height="wrap_content"
android:layout_below="@id/textOne" />
</RelativeLayout>
创建一个硬尺寸,因为宽度违背了让主元素换行的目的。是否有某人知道的技术或我错过的属性来实现此目的?可以是任何标准布局,我只是选择 RelativeLayout
作为示例。
此外,如果元素彼此不靠近怎么办?
干杯。
I'm looking for some insights into the layout system. Is anyone aware of a method in the Android layout system that allows peer View
elements (meaning Views grouped together in the same container) to match their height and width attributes without creating an interstitial container just to govern their sizes?
For example: A layout has two TextView
elements. The first should always wrap width to its text content, but the second should match the width of the first. A solution that WORKS is this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textTwo"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
However, I had to create a special container for both views to ensure they both match the width of whatever text is in the first; adding unnecessary complexity to the hierarchy. What I would love to know is if there is a layout attribute I could use to do something like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textTwo"
android:layout_widthMatchPeer="@id/textOne" <!-- An attribute like this -->
android:layout_height="wrap_content"
android:layout_below="@id/textOne" />
</RelativeLayout>
Creating a hard dimension as the width is defeating the purpose of letting the primary element wrap. Is there a technique someone is aware of, or an attribute I've missed, to accomplish this? Could be in any standard layout, I just chose RelativeLayout
as an example.
Additionally, what if the elements are not located near each other?
Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为
RelativeLayout
类可以做你想做的事:I think the
RelativeLayout
class can do what you want:由于视图处于相对布局中,因此使用 android:layout_below="id/firsttv" android:layout_alignLeft="id/firsttv" 和 android:layout_alightRight="id/firsttv"
since the views are in a relatve layout, use android:layout_below="id/firsttv" android:layout_alignLeft="id/firsttv" and android:layout_alightRight="id/firsttv"