在RelativeLayout中使用layout_above

时间:2011-05-11 21:21:36

标签: android android-relativelayout

有人可以向我解释为什么ImageView没有出现在LinearLayout上面吗?

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:id="@+id/rev_main"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <!-- some stuff in here -->
    </LinearLayout>
    <ImageView
        android:id="@+id/rev_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow"
        android:layout_above="@id/rev_main"
        />
</RelativeLayout>

我不明白。

4 个答案:

答案 0 :(得分:25)

当您指定相对于另一个布局的对齐时,会发生这种情况。我发现的解决方案是走向另一个方向。

不要告诉ImageView在LinearLayout之上,而是告诉LinearLayout在ImageView之下。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:id="@+id/rev_main"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rev_arrow">
        <!-- some stuff in here -->
    </LinearLayout>
    <ImageView
        android:id="@+id/rev_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow"
        />
</RelativeLayout>

答案 1 :(得分:7)

以下内容对您有用:

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/rev_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow"
        android:layout_alignParentTop="true"
        />
    <LinearLayout
        android:id="@+id/rev_main"
        android:layout_width="fill_parent"
        android:layout_below="@id/rev_arrow"
        android:layout_height="wrap_content">
        <!-- some stuff in here -->
    </LinearLayout>
</RelativeLayout>

答案 2 :(得分:1)

我通过创建自定义optionsMenu遇到了类似的问题。设置z顺序视图的最简单方法是以编程方式执行。如果您想有时切换订单,您应该轻松致电:

    ImageView yourImageView = (ImageView)findViewById(R.id.rev_arrow);
    yourImageView.bringToFront();

我不知道它是否适合您的应用程序,但在我的情况下它完美无缺。如果您需要更多代码,请与我们联系。

答案 3 :(得分:0)

如果您在ListView中遇到此类问题,请确保使用适当的充气方法。必须指定父视图组以确保正确的通货膨胀。

mLayoutInflater.inflate(R.layout.listitem, parent, false);

请勿使用

mLayoutInflater.inflate(R.layout.listitem, null);