【问题标题】:layout_height="fill_parent" in RelativeLayout doesn't work相对布局中的 layout_height="fill_parent" 不起作用
【发布时间】:2012-07-13 01:58:46
【问题描述】:

我有以下布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/item_background"
    android:layout_margin="10dip"
    android:gravity="center_vertical">

    <View
        android:layout_width="30dip"
        android:layout_height="fill_parent"
        android:background="@color/test_color"/>

    <ImageView
        android:id="@+id/task_item_startstop_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/start_task"
        android:layout_centerVertical="true"
        android:focusable="false"
        android:background="#0000"/>

    <View
        android:id="@+id/task_item_line"
        android:layout_width="2dp"
        android:layout_height="40dip"
        android:layout_margin="5dip"
        android:background="@color/line_color"
        android:layout_toRightOf="@id/task_item_startstop_button"/>

    <TextView
        android:id="@+id/task_item_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:textSize="22sp"
        android:textStyle="bold"
        android:layout_toRightOf="@id/task_item_line" />

    <TextView
        android:id="@+id/task_item_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/task_item_suspended"
        android:textSize="18sp"
        android:textColor="@android:color/black"
        android:layout_toRightOf="@id/task_item_line"
        android:layout_below="@id/task_item_title"/>

    <TextView
        android:id="@+id/task_item_timer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dip"
        android:textSize="18sp"
        android:textColor="@android:color/black"
        android:layout_toRightOf="@id/task_item_status"
        android:layout_below="@id/task_item_title"/>

</RelativeLayout>

我想在布局的左侧有垂直线(根布局内的第一个视图),固定宽度和高度等于父级的高度。但是如果我设置 layout_height="fill_parent" 它没有帮助。高度为 0。如果我设置高度,例如 40dip,它将是 40dip。但我需要的正是 fill_parent 属性。

【问题讨论】:

  • 你能发一张你想要达到的效果图吗?
  • 试试这个,使用 android:layout_alignParentLeft="true" 作为你的视图,然后设置 task_item_startstop_button 相对于它。给一个 id 来查看
  • @AleksG 我需要在此布局的左侧有垂直条
  • @Shubhayu 它没有帮助
  • 嘿,我也面临同样的问题,我需要左侧条,但上面的代码我试过它没有任何线索为什么视图不采用 fill_parent 作为高度属性?,如果我将高度硬编码为 10 或20 次浸就可以了。

标签: android android-relativelayout fill-parent


【解决方案1】:

这能解决您的问题吗?只需用您自己的替换相关元素

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

        <Button 
            android:layout_height="match_parent"
            android:layout_width="30dp"
            android:id="@+id/thebutton"
            android:text="hello world"
            />

    <AnalogClock
        android:id="@+id/analogClock1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/thebutton" />

</RelativeLayout>

【讨论】:

    【解决方案2】:

    您已经为 Parent 相对布局提供了边距,这就是为什么每当您添加任何内容时,它总是为您的小部件提供边距。 从您的父级相对布局中删除 android:layout_margin="10dip" 然后您会得到 fil 父级中的行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-04
      • 2019-12-31
      • 2014-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-12
      相关资源
      最近更新 更多