【问题标题】:Setting View Height In Relative Layout在相对布局中设置视图高度
【发布时间】:2012-06-18 19:22:18
【问题描述】:

我有一个这样的布局,我想在视图上方显示一个标签,然后在下面重复它。

这是我的代码...

<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_weight,但它不喜欢它嵌套在另一个内部的事实。

谁能看到这个问题的另一种解决方案?

【问题讨论】:

  • 我不明白你的问题,你能澄清一下吗?
  • 我希望它们的大小相同,但我不想设置特定的大小,因为我想垂直填充整个屏幕。所以我不想使用 40dp 或其他尺寸,也许是百分比。
  • @DiscoS2 所以你基本上是想让TextView+View的每组占据父RelativeLayout的一半高度?
  • 是的,没错。 layout_weight 会很理想,我只是收到警告
  • 我添加了一个答案。看看吧。

标签: android android-layout android-relativelayout


【解决方案1】:

您可以通过一个小技巧来避免双重权重问题。您可以在父RelativeLayout 的垂直中心设置一个空的View,并将两个组定位在View 的上方和下方,如下所示:

<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>

此外,您应该仅在第一次声明该 id 时使用 @+id/... 表示法,在下一次出现 id 时,您应该使用 @id/...。

【讨论】:

  • 你应该使用“Space”而不是View作为锚点,Space节省更多的内存和资源。
【解决方案2】:

可以通过layout_weight实现动态屏幕大小。但在此之前,您只需尝试 layout_weight 到线性布局并为视图和文本视图提供适当的权重。它几乎可以解决您的问题。不要使用带权重的相对布局。 weight 在相对布局中不起作用。

【讨论】:

  • 我已经使用了带权重的线性布局,问题是我收到关于使用嵌套 layout_weight 标签的警告,说它对性能不利,所以我试图找到另一种方法,因此转向相对布局。
  • js lint 显示该警告。它不会影响你的表现。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 2012-01-11
  • 2017-12-17
  • 1970-01-01
  • 1970-01-01
  • 2013-02-02
相关资源
最近更新 更多