Android 在相对布局中设置视图高度
我有一个这样的布局,我想在视图上面显示一个标签,然后在下面重复它 这是我的密码Android 在相对布局中设置视图高度,android,android-layout,android-relativelayout,Android,Android Layout,Android Relativelayout,我有一个这样的布局,我想在视图上面显示一个标签,然后在下面重复它 这是我的密码 <RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical" > <TextView android:id="@+id/speedL
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/speedLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="@string/speed"
android:textAppearance="?android:attr/textAppearanceLarge" />
<View
android:id="@+id/speedGraph"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginBottom="4dp"
android:layout_below="@+id/speedLbl"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@color/blueColour" />
<TextView
android:id="@+id/distanceLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_below="@+id/speedGraph"
android:text="@string/distance"
android:textAppearance="?android:attr/textAppearanceLarge" />
<View
android:id="@+id/distanceGraph"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="@+id/distanceLbl"
android:layout_marginBottom="4dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@color/blueColour" />
</RelativeLayout>
问题是我不想为这两个视图设置高度,因为我希望它们根据屏幕大小动态变化我尝试使用
layout\u weight
但它不喜欢它嵌套在另一个布局中的事实
有人能找到解决这个问题的另一种方法吗?您可以通过
布局\权重
实现动态屏幕大小。但在此之前,您只需尝试对线性布局设置layou weight
,并为视图和文本视图提供适当的权重。它几乎解决了你的问题。不要使用带有权重的相对布局。权重在相对布局中不起作用。您可以通过一个小技巧避免双重权重问题。您可以在父视图的垂直中心设置一个空的视图
,并将两个组放置在该视图
的上方和下方,如下所示:
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<View android:id="@+id/anchor"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_centerVertical="true/>
<TextView
android:id="@+id/speedLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="@string/speed"
android:textAppearance="?android:attr/textAppearanceLarge" />
<View
android:id="@+id/speedGraph"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginBottom="4dp"
android:layout_below="@id/speedLbl"
android:layout_above="@id/anchor"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@color/blueColour" />
<TextView
android:id="@+id/distanceLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_below="@id/anchor"
android:text="@string/distance"
android:textAppearance="?android:attr/textAppearanceLarge" />
<View
android:id="@+id/distanceGraph"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="@id/distanceLbl"
android:layout_alignParentBottom="true"
android:layout_marginBottom="4dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@color/blueColour" />
</RelativeLayout>
我不明白你的问题,你能澄清一下吗?我希望它们的大小相同,但我不想设置特定的大小,因为我想垂直填充整个屏幕。所以我不想使用40dp或其他大小,可能是一个百分比。@DiscoS2所以你基本上希望每组TextView
+View
占据父母相对年轻人身高的一半?是的。布局和重量将是理想的,我只是得到一个警告,我已经添加了一个答案。检查一下。我已经使用了一个带有权重的线性布局,问题是我收到了一条关于使用嵌套布局的警告,说这对性能不好,所以我正在尝试另一种方法,因此转到relative layouts.js lint显示了这条警告。它不会影响您的性能。您应该使用“空间”而不是视图作为锚定,空间可以节省更多内存和资源。