【问题标题】:Layout below not properly working in Relativelayout下面的布局在 Relativelayout 中无法正常工作
【发布时间】:2017-03-31 18:00:02
【问题描述】:

首先请检查这个xml代码:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/adass"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/img_channel"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/img_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:scaleType="fitXY"
            android:src="@drawable/selector_add" />

        <LinearLayout
            android:id="@+id/lnmr"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:id="@+id/txt_Channel_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:text="My Channel"
                android:textColor="@color/black"
                android:textSize="@dimen/text_size_menu" />

            <TextView
                android:id="@+id/txt_Category_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:text="Category Name"
                android:textColor="@color/gray" />
        </LinearLayout>
    </LinearLayout>

    <de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/img_channel"
        android:layout_width="@dimen/add_channel_logo_height"
        android:layout_height="@dimen/add_channel_logo_height"
        android:layout_alignParentRight="true"
        android:src="@drawable/add_channel_placeholder"
        app:civ_border_color="@color/white"
        app:civ_border_width="2dp" />
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_below="@id/adass"
        android:layout_toLeftOf="@id/img_channel"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="@color/light_gray" />
</RelativeLayout>

这是输出http://prntscr.com/d8ccq7

谢谢!

【问题讨论】:

  • 我不确定将@id/adass 替换为@+id/adass 并尝试?
  • @Raghavendra 我两种方式都试过了
  • 澄清你的问题!!

标签: android xml layout android-linearlayout android-relativelayout


【解决方案1】:

在代码中的任何地方都应该使用android:layout_below="@+id/your_layout_id" 引用布局,确保你使用@+id 而不是@id

 <View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:layout_below="@+id/adass"
    android:background="@color/light_gray" />

试试这是否有帮助,如果有帮助,您可以稍后添加边距和左对齐

【讨论】:

  • 我在视图中同时尝试了@+id 和@id,但没有成功
【解决方案2】:

我遇到了同样的问题,发现 toLeftOf/toRightOf 的顺序取决于布局 xml 中子项的顺序。作为初始化 id 的示例,您必须使用 @+id/something 中的“+”,然后才能将视图关联为 @id/something 更下方。我希望这是有道理的。我相信如果您将布局更改为:

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/adass"
    android:layout_centerVertical="true"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/img_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:scaleType="fitXY"
        android:src="@drawable/selector_add" />

    <LinearLayout
        android:id="@+id/lnmr"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txt_Channel_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="My Channel"
            android:textColor="@color/black"
            android:textSize="@dimen/text_size_menu" />

        <TextView
            android:id="@+id/txt_Category_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="Category Name"
            android:textColor="@color/gray" />
    </LinearLayout>
</LinearLayout>

<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/img_channel"
    android:layout_width="@dimen/add_channel_logo_height"
    android:layout_height="@dimen/add_channel_logo_height"
    android:layout_alignParentRight="true"
    android:src="@drawable/add_channel_placeholder"
    app:civ_border_color="@color/white"
    app:civ_border_width="2dp" 
    android:layout_toRightOf="@id/adass" !!!!
/>
<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:layout_below="@id/adass"
    android:layout_toLeftOf="@id/img_channel"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="@color/light_gray" />

这应该可行。 :)

【讨论】:

    【解决方案3】:

    添加这一行

     <view ....
    android:layout_below="@id/adass"/>
    

    【讨论】:

    • 这不是解决方案,亲爱的!
    • 你的预期结果是什么
    • 我可以使用你的代码并且只改变几个地方,你能告诉我预期的输出(屏幕截图)
    • 我想在线性布局下方显示此视图,但它显示在中心垂直位置。请检查prntscr.com/d8ccq7这是意外行为
    猜你喜欢
    • 2012-10-10
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    相关资源
    最近更新 更多