【问题标题】:Set foreground to imageview将前景设置为图像视图
【发布时间】:2016-10-12 07:32:23
【问题描述】:

我将前景设置为我的图像视图,以便在它被选中时进行选择。这在 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>

【问题讨论】:

    标签: android shape selecteditem foreground


    【解决方案1】:

    我有办法给你

    选择器应该是这样的

    <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

    【讨论】:

      【解决方案2】:
              case R.styleable.View_foreground:
                  if (targetSdkVersion >= Build.VERSION_CODES.M || this instanceof FrameLayout) {
                      setForeground(a.getDrawable(attr));
                  }
                  break;
      

      这是 View.java 初始化的代码。前台仅适用于 API-23+ 和 FrameLayout

      【讨论】:

        【解决方案3】:

        build.gradle file 中添加此代码并重建

        compile 'com.android.support:multidex:1.0.0' 
        

        我希望这会对你有所帮助。 GL :)
        发现自this problem

        【讨论】:

          猜你喜欢
          • 2013-10-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-02
          • 1970-01-01
          • 2017-01-21
          • 1970-01-01
          • 2018-06-09
          相关资源
          最近更新 更多