2011-05-17 58 views
2

我正在寻找一些布局系统的见解。有没有人知道Android布局系统中的一种方法,允许对等点View元素(表示视图在同一容器中分组在一起)匹配其高度和宽度属性,而不创建仅用于管理其大小的插入式容器?Android布局匹配对象

例如:布局有两个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作为示例。

此外,如果元素不在彼此附近?

干杯。

回答

5

我觉得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_alignLeft="@id/textOne" 
    android:layout_alignRight="@id/textOne" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/textOne" /> 
</RelativeLayout> 
+0

是的,这个作品 – 2011-05-17 17:03:13

+1

这非常有帮助,谢谢。您是否知道在视图未定位时要使用的方法,以便它们可以共享边缘坐标,但只需匹配其大小?干杯。 – Devunwired 2011-05-17 17:47:28

+0

不客气!我认为Android没有一个通用的解决方案。 – Michael 2011-05-17 19:23:59

0

因为意见是在relatve布局,采用Android:layout_below = “ID/firsttv” 机器人:layout_alignLeft = “ID/firsttv”和android:layout_alightRight =“id/firsttv”