• 动态更换view类的背景----StateListDrawable的应用


    StateListDrawable可以根据View的不同状态,更换不同的背景

    可以应用如EditText,Button等中,以Button为例 

    系统中默认的按钮被按下的颜色和未点击时的颜色不一样,该种实现可以用Java代码和XML实现

    以Java代码:

    //……前面对Button的声明略去
    okBtn.setBackgroundDrawable(addStateDrawable(this, R.drawable.btn_normal, R.drawable.btn_selected, R.drawable.btn_selected));
    cancelBtn.setBackgroundDrawable(addStateDrawable(this, R.drawable.btn_normal, R.drawable.btn_selected, R.drawable.btn_selected));
    
    //……对应主要的代码
     //当对应的View处于不同的状态时,对应的bacdground跟着变化
        private StateListDrawable addStateDrawable(Context context,  int idNormal, int idPressed, int idFocused) {
            StateListDrawable sd = new StateListDrawable();
            Drawable normal = idNormal == -1 ? null : context.getResources().getDrawable(idNormal);
            Drawable pressed = idPressed == -1 ? null : context.getResources().getDrawable(idPressed);
            Drawable focus = idFocused == -1 ? null : context.getResources().getDrawable(idFocused);
            //注意该处的顺序,只要有一个状态与之相配,背景就会被换掉
            //所以不要把大范围放在前面了,如果sd.addState(new[]{},normal)放在第一个的话,就没有什么效果了 
            sd.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_focused}, focus);
            sd.addState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled}, pressed);
            sd.addState(new int[]{android.R.attr.state_focused}, focus);
            sd.addState(new int[]{android.R.attr.state_pressed}, pressed);
            sd.addState(new int[]{android.R.attr.state_enabled}, normal);
            sd.addState(new int[]{}, normal);
            return sd;
        }


    xml方式实现时,可以先了解下对应xml的语法 

    <?xml version="1.0" encoding="utf-8"?>
    
    <selector xmlns:android="http://schemas.android.com/apk/res/android"
    
    android:constantSize=["true" | "false"]
    
    android:dither=["true" | "false"]
    
    android:variablePadding=["true" | "false"] >
    
    <item
    
    android:drawable="@[package:]drawable/drawable_resource"
    
    android:state_pressed=["true" | "false"]
    
    android:state_focused=["true" | "false"]
    
    android:state_selected=["true" | "false"]
    
    android:state_active=["true" | "false"]
    
    android:state_checkable=["true" | "false"]
    
    android:state_checked=["true" | "false"]
    
    android:state_enabled=["true" | "false"]
    
    android:state_window_focused=["true" | "false"] />
    
    </selector>

    下面对应的具体实例,由于是做背景用,该xml将放于/res/drawable下面(StateList中第一个匹配当前状态的item会被使用。因此,如果第一个item没有任何状态特性的话,那么它将每次都被使用,这也是为什么默认的值必须总是在最后)

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" android:drawable="@drawable/btn_selected"/>
        <item android:state_focused="true" android:drawable="@drawable/btn_selected"/>
        <item android:state_enabled="true" android:drawable="@drawable/btn_normal"/>
        <item  android:drawable="@drawable/btn_normal" />
    </selector>
        

    在Button的xml中进行加载:

     <Button
                android:id="@+id/canel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:text="@string/btn_cancel"
                android:layout_margin="10dip"
                android:layout_weight="1"
                android:textColor="#ffffffff"
                android:textSize="15sp"
                android:background="@drawable/button_drawable"
                />

     或在java代码中加载:

    okBtn.setBackgroundDrawable(R.drawable.button_drawable);  
  • 相关阅读:
    poj 1080 dp
    Codeforces Round #280 (Div. 2)
    Codeforces Round #279 (Div. 2)f
    Codeforces Round #278 (Div. 1)
    Codeforces Round #276 (Div. 1)
    搜索
    debug 心得
    ZOJ 1633
    DRF 一对多序列化与反序列化
    HTTP协议
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/5265378.html
Copyright © 2020-2023  润新知