Android 将前景设置为imageview

Android 将前景设置为imageview,android,shape,selecteditem,foreground,Android,Shape,Selecteditem,Foreground,我将“前景”设置为“我的图像视图”,以便在选择它时进行选择。这在android 6版本中运行良好。启用时,选择“不更改最小版本”。请帮帮我 <ImageView android:id="@+id/style_list_item_image_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_g

我将“前景”设置为“我的图像视图”,以便在选择它时进行选择。这在android 6版本中运行良好。启用时,选择“不更改最小版本”。请帮帮我

<ImageView
        android:id="@+id/style_list_item_image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:foreground="@drawable/style_item_selector"
        android:scaleType="center"
        android:drawSelectorOnTop="true"
        android:clipChildren="true"
        android:clipToPadding="true"/>

style_item_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true">
        <shape xmlns:android="http://schemas.android.com/apk/res/android">
            <size android:width="60dp" android:height="80dp" />
            <stroke android:width="1dp" android:color="#ff3591" />
            <solid android:color="@color/transparent100" />
            <corners android:radius="1dp" />
        </shape>
    </item>
</selector>

build.gradle文件中添加此代码并重新生成

compile 'com.android.support:multidex:1.0.0' 
我希望这对你有帮助。德国劳埃德船级社:)

从中找到我有一个解决方案给你

选择器将如下所示

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <size android:width="60dp" android:height="80dp" />
        <stroke android:width="1dp" android:color="#ff3591" />
        <solid android:color="@android:color/transparent" />
        <corners android:radius="1dp" />
    </shape>
</item>

ImageView需要一些总结

<FrameLayout
    android:id="@+id/share"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:foreground="@drawable/style_item_selector">


    <ImageView
    android:id="@+id/style_list_item_image_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:adjustViewBounds="true"
    android:scaleType="center"
    android:drawSelectorOnTop="true"
    android:clipChildren="true"
    android:clipToPadding="true"/>
</FrameLayout>

希望这对你有用

确保将onClickListener附加到FrameLayout

        case R.styleable.View_foreground:
            if (targetSdkVersion >= Build.VERSION_CODES.M || this instanceof FrameLayout) {
                setForeground(a.getDrawable(attr));
            }
            break;
这是View.java初始化中的代码。前台仅适用于API-23+和FrameLayout