Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.0k views
in Technique[技术] by (71.8m points)

android - How to get an ImageView on top of a LinearLayout in RelativeLayout

I have a LinearLayout that replicates a button, and I'd like to add an ImageView to the top right corner of this button. So, I decided to place this LinearLayout inside a RelativeLayout and add the ImageView below the LinearLayout aligning it with respect to the LinearLayout. It works great, except the ImageView appears to be below the LinearLayout. How do I fix this?

My code:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/timetable"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="10dp"
        android:background="@drawable/button_card"
        android:clickable="true"
        android:focusable="true"
        android:gravity="center_vertical"
        android:onClick="openTimetable"
        android:orientation="vertical"
        android:stateListAnimator="@animator/item_elevation">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="false"
            android:contentDescription="@string/timetable_description"
            app:srcCompat="@drawable/ic_timetable"
            app:tint="@color/colorPrimary" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:clickable="false"
            android:text="@string/timetable"
            android:textAlignment="center"
            android:textColor="@color/colorPrimary"
            android:textStyle="bold" />
    </LinearLayout>

    <!-- This is the view that I want to be paced on top of the LinearLayout -->
    <ImageView
        android:id="@+id/timetable_notification"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/timetable"
        android:layout_alignEnd="@id/timetable"
        android:contentDescription="@string/notification_description"
        android:src="@drawable/ic_notification_dot" />
</RelativeLayout>

The above code gives this:

enter image description here

The red dot on the top right corner is the ImageView. I want it to be placed on top on the LinearLayout.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The cause for a problem is an elevation given to Linearlayout

android:stateListAnimator="@animator/item_elevation"

This elevation sets Z-order on top of the Imageview and hence You are seeing an Imageview behind the LinearLayout.

So applying the same elevation on Imageview also would resolve an issue


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...