Android 5.0以下XML定义的drawable不识别?attr/属性的解决思路

来源:互联网 发布:软件需求 功能需求 编辑:程序博客网 时间:2024/06/10 18:14

最近写个小项目,之前一直使用5.0 和7.0系统的手机测试正常,换到4.4系统却报Resources$NotFoundException错误,很是疑惑,因为错误指向的drawable文件正常啊,把log复制到搜索引擎查了一下,也没什么收获,但查到有个类似的问题,是:TextView.setText()时,直接给了一个int型的参数,导致系统把这个参数当做Resource id去处理了。 根据这个思路,我打开我的drawable文件,试着把里面的"?attr/colorPrimary"换成“@color/颜色资源”,结果没有报错。网上查了一下,果然是5.0以下,在drawable中无法正确引用?attr/的值(我的想法是:系统本应该取到颜色值,可却把这个值做resource id处理了,然后根据id去寻找资源时,没有找到资源)。

可因为我在软件中加入了主题更换的功能,所以要获取实时的?attr/colorPrimary,而不是固定的color资源,于是想了些解决办法:

 1、新建drawable-21 文件夹,对于5.0和5.0 以下系统的drawable资源分别定义,这样可以解决,但是分别定义的drawable因为使用的资源的不同可能导致效果不同。

2、逻辑上避开在drawable中引用?attr/资源。比如我在某一个按钮上的效果:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:layout_width="96dp"        android:layout_height="40dp"        android:layout_centerInParent="true"        android:background="@drawable/btn_pressed"        android:text="按钮"        android:textColor="#FFFFFF"/></RelativeLayout>

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="false">        <shape android:shape="rectangle">            <solid android:color="?colorPrimary"/>        </shape>    </item>    <item android:state_pressed="true">        <shape android:shape="rectangle">            <solid android:color="?colorPrimaryDark"/>        </shape>    </item></selector>
很简单,就不上图了,96*48的按钮,触摸时和不触摸时引用不同的颜色。

可在5.0 以下系统,肯定会报Resources$NotFoundException,怎么解决呢?

我们可以在这个按钮下面放一个同样大小的控件,任何设置该控件的background为?colorPrimary。然后在按钮的drawable文件中,将不触摸时的颜色设为全透明,触摸时设为一定透明度的黑色。如下:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <FrameLayout        android:layout_width="96dp"        android:layout_height="40dp"        android:layout_centerInParent="true"        android:background="?colorPrimary">        <Button            android:layout_width="96dp"            android:layout_height="40dp"            android:background="@drawable/btn_pressed"            android:text="按钮"            android:textColor="#FFFFFF"/>    </FrameLayout></RelativeLayout>
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="false">        <shape android:shape="rectangle">            <!--全透明色-->            <solid android:color="#00000000"/>        </shape>    </item>    <item android:state_pressed="true">        <shape android:shape="rectangle">            <!--透明度为12的黑色-->            <solid android:color="#1E000000"/>        </shape>    </item></selector>
两次的效果是差不多的,通过调整透明色,可以达到一样的效果。这样,我们避开了在drawable中引用?attr的资源,而且到达预期的效果了。
3、在java代码中定义drawable。这也是最好的一种解决办法,我们可以在代码中获取到?attr/资源的值,然后在定义的drawable中引用就行了,

public class TestActivity extends AppCompatActivity{    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_test);        Utils.context=this;        Button button=(Button)findViewById(R.id.btn);        button.setBackground(getStateListDrawable());    }//    对应drawable 中的selector    private StateListDrawable getStateListDrawable(){        StateListDrawable stateListDrawable=new StateListDrawable();        stateListDrawable.addState(new int[]{-android.R.attr.state_pressed},getGradientDrawable(false));        stateListDrawable.addState(new int[]{android.R.attr.state_pressed},getGradientDrawable(true));        return stateListDrawable;    }//    对应drawable 中的 shape    private GradientDrawable getGradientDrawable(boolean isPressed){        GradientDrawable gradientDrawable=new GradientDrawable();        gradientDrawable.setShape(GradientDrawable.RECTANGLE);        // 获取颜色        TypedValue primaryValue=new TypedValue();        TypedValue primaryDarkValue=new TypedValue();        this.getTheme().resolveAttribute(R.attr.colorPrimary,primaryValue,true);        this.getTheme().resolveAttribute(R.attr.colorPrimaryDark,primaryDarkValue,true);//        背景颜色        if(isPressed){            gradientDrawable.setColor(primaryDarkValue.data);        } else {            gradientDrawable.setColor(primaryValue.data);        }        gradientDrawable.setBounds(0,0,SizeUtils.dp2px(96),SizeUtils.dp2px(48));        return gradientDrawable;    }}
代码很简单,可以对应在xml中定义drawable的方式来看,实现的效果和之前的一样。关于上面用到的
StateListDrawable  
GradientDrawable

两个类,大家可以去详细学习一下,我这里知识提一下解决思路,如果你有更好的方法,可以在下面评论告诉我。我也是新手,临近毕业,连工作都没找到大哭
还是上传个图片吧,上面实现的效果:



1 0
原创粉丝点击