【问题标题】:Selector only works on android 6选择器仅适用于 android 6
【发布时间】:2016-06-14 14:39:59
【问题描述】:

我在布局中添加了一个选择器,当按下布局时应该添加一个白色边框。它按预期工作,但仅适用于 Android 6.0 并且较旧的设备没有突出显示。我尝试在清单中添加 android:hardwareAccelerated="true",但这并没有什么不同。

有人知道为什么这不适用于旧的 android 版本吗?

突出显示的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:foreground="@drawable/selector">

选择器

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
    android:drawable="@drawable/border_white_rounded" />

边框

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/transparent" />

<stroke
    android:width="3dp"
    android:color="@color/color_white" />

<corners android:radius="3dp" />

【问题讨论】:

  • 我相信前景比较新,你检查过它的兼容性支持吗?

标签: android


【解决方案1】:

虽然android:foreground 自 API 1 以来就存在,但 it was only moved up to the View class in API 23 (Marshmallow, 6.0)

从 API 1 到 API 23,android:foreground 仅适用于 FrameLayout

这是一个类似的问题,其中包含一些在早期版本的 Android 中支持前台的解决方案:How to set foreground attribute to other non FrameLayout view

【讨论】:

  • 感谢您的澄清,并提供有效的解决方案!
猜你喜欢
  • 2014-05-29
  • 2016-12-22
  • 2012-02-23
  • 2013-05-31
  • 2021-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-17
相关资源
最近更新 更多