如何将前景属性设置为其他非FrameLayout视图

ces*_*rds 13 android view drawable android-view android-framelayout

我想知道如何在不同于FrameLayout的视图中应用或模拟前景效果,如LinearLayout或RelativeLayout

这就是我现在拥有的:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cardContent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/row_background"
    android:foreground="@drawable/foreground_row">

    ...

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

我想要的东西是这样的:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cardContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/row_background"
        app:foreground="@drawable/foreground_row">

        ...

    </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

提前致谢!!

小智 19

我们的想法是使用FrameLayout包围您的布局,并将选择器和onClick事件设置为此布局.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/selectableItem"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:foreground="@drawable/foreground_row"
    >

   <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cardContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/row_background">

        ...

    </RelativeLayout>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

你可以在我的博客上找到完整的解释:

http://antonioleiva.com/unveiling-bandhook-foreground-any-layout/

或者你可以扩展rhis FRelativeLayout https://gist.github.com/shakalaca/6199283