当使用layout_above时,Android ImageView会消失

4

我正在使用RelativeLayout,尝试在图像下方放置几个按钮。但是,当我在ImageView中使用 android:layout_above="@+id/likebutton" 时,为什么会使ImageView消失?如何使其不消失?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/listitem_shape" >

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="15dp"
    android:layout_marginBottom="10dp" />

<ProgressBar
    android:id="@+id/progressbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<ImageView
    android:id="@+id/imageview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/likebutton"
    android:layout_below="@+id/title" />

<ImageButton
    android:id="@+id/likebutton"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:background="@drawable/like"/>

<ImageButton
    android:id="@+id/sharebutton"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/likebutton"
    android:background="@drawable/share" />
</RelativeLayout >
2个回答

5

您正在设置 android:layout_alignParentBottom="true",因此您无法使用 android:layout_above="@+id/likebutton"。简而言之,当您将某些子视图设置为相对于父视图时,您不能设置其他视图相对于它。否则它不会反映出来。


你能提供一些关于如何找到相关文档的指示吗?大多数情况下,当你使用RelativeLayout时,你会将某些东西设置在顶部、左侧、底部等位置,然后相对于它设置其他东西。我不明白如何在没有与父视图相对的视图的情况下进行布局。 - Charles

4

试一试这个

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/listitem_shape"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/ll1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="15dp"
            android:textStyle="bold" />

        <ProgressBar
            android:id="@+id/progressbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <ImageView
            android:id="@+id/imageview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/likebutton"
            android:layout_below="@+id/title" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageButton
            android:id="@+id/likebutton"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:background="@drawable/like" />

        <ImageButton
            android:id="@+id/sharebutton"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:layout_toRightOf="@+id/likebutton"
            android:background="@drawable/share" />
    </LinearLayout>

</LinearLayout>

希望这能有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接