Android 在RelativeLayout中使用上面的布局_

Android 在RelativeLayout中使用上面的布局_,android,android-relativelayout,Android,Android Relativelayout,有人能给我解释一下为什么ImageView没有出现在线性布局上方吗 <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/rev_main" android:layout_width="fill_parent" androi

有人能给我解释一下为什么ImageView没有出现在线性布局上方吗

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


我不明白。

以下内容应该适合您:

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

当您指定相对于另一布局的对齐方式时,会发生这种情况。我找到的解决办法是往另一个方向走

不要告诉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>

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

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

我不知道它是否适合您的应用程序,但就我而言,它工作得非常完美。如果您需要更多代码,请告诉我。

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

mLayoutInflater.inflate(R.layout.listitem, parent, false);
不要使用

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

它出现在任何地方吗?什么地方都没有?也许贴一张照片看看它是什么样子,这样就更容易说出原因了。在我的布局中,我使用像“@+id/rev_main”这样的id,即使在上面的布局_这样的属性中也是如此。老实说,虽然我甚至不确定两者之间的区别,也许试试看?它消失了。如果我首先声明ImageView并尝试使LinearLayout自身位于ImageView之上,LinearLayout也会消失。仅供参考-listview项布局中存在此问题。在我为项目布局应用了固定高度后,问题就消失了。我的问题是:用于ImageView的可绘制表格发生了更改。这意味着我需要在代码中为它设置一个变量。线性布局的位置(高于或低于)也会发生变化。如果我需要编辑LinearLayout的LayoutParams,这意味着我还需要一个用于LinearLayout的变量。我希望ImageView只需要一个变量。嗯,变量很便宜。或者,您可以创建两个图像视图,一个在上,一个在下,并将初始状态设置为android:visibility=“gone”。然后,您可以根据需要通过编程更改每个视图的可见性状态。您不也需要将rev_arrow图像视图移动到xml中线性布局的上方吗?否则,您将得到一个引用错误,因为文件是线性解析的。我刚刚遇到了那个问题-PI将一个视图与其他视图对齐,并希望在其上方有其他视图:我必须使用上面的
layout\u
。通过将这两个设置为
线性布局解决了此问题
我对此有问题。有时,我希望图像视图上方的线性布局。当我将LinearLayout上的属性更改为上面的layout_时,LinearLayout将完全消失。