【问题标题】:RelativeLayout: can't use layout_below with link to a LinearLayout child?RelativeLayout:不能将 layout_below 与指向 LinearLayout 子项的链接一起使用?
【发布时间】:2011-02-10 22:54:20
【问题描述】:

我有一个 RelativeLayout,它有一个 LinearLayout 作为包含 TextViews 的子级。

我想用 layout_alignRight="@id/userdetail_tv_special" 将 ImageViews 放置在 TextViews 的右侧,但不知何故这不起作用。

我不能将 RelativeLayout 的子代“链接”到 LinearLayout 的子代吗?有什么想法可以在不为每个按钮创建另一个 RelativeLayout 的情况下实现这一点吗?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
    <LinearLayout
        android:id="@+id/userdetail_lin_btncontainer"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_below="@id/userdetail_lin_divider" 
        android:padding="6dp" >
        <TextView
            android:id="@+id/userdetail_tv_special"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="Special"
            android:background="#cccccc" />
        <!-- here are more TextViews like this one -->
    </LinearLayout>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@id/userdetail_tv_special"
        android:src="@drawable/ic_detail_special" />
        <!-- much more views and stuff -->
</RelativeLayout>

【问题讨论】:

    标签: android android-linearlayout android-relativelayout


    【解决方案1】:

    在我看来,您不能指向更深层次的“层”,这似乎是合乎逻辑的:您可以将 RelativeLayout 的子级相互布局,但您的 LinearLayout 是一个完整的视图,您可以将其用作参考。

    如果您希望能够相对于 textview 定位视图,请将它们放置为 relativelayout 的直接子级。顺便说一句,我不明白你为什么需要一个 LinearLayout。

    【讨论】:

    • LinearLayout 有 5 个 TextView,layout_width=fill_parent 和 layout_weigth=1 可以拉伸它们以填充屏幕的整个宽度......还有其他可能吗?
    【解决方案2】:
     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent">
      <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imgview"
        android:src="@drawable/ic_detail_special" />
    
    <LinearLayout
        android:id="@+id/userdetail_lin_btncontainer"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_below="@id/userdetail_lin_divider" 
       android:layout_alignLeft="@id/imgview"
        android:padding="6dp" >
        <TextView
            android:id="@+id/userdetail_tv_special"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="Special"
            android:background="#cccccc" />
        <!-- here are more TextViews like this one -->
    </LinearLayout>
    
          <!-- much more views and stuff -->
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-18
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-26
      相关资源
      最近更新 更多