• 3.02StateListDrawable_Android


    TOC

    StateListDrawable_Android

    StateListDrawable 是Drawable资源的一种,可以根据不同的状态,设置不同的图片效果,关键节点 < selector > ,我们只需要将Button的 background 属性设置为该drawable资源即可轻松实现,按下 按钮时不同的按钮颜色或背景!

    属性名 说明
    drawable 引用的Drawable位图,我们可以把他放到最前面,就表示组件的正常状态~
    state_focused 是否获得焦点
    state_window_focused 是否获得窗口焦点
    state_enabled 控件是否可用
    state_checkable 控件可否被勾选
    state_checked 控件是否被勾选
    state_selected 控件是否被选择,针对有滚轮的情况
    state_pressed 控件是否被按下
    state_active 控件是否处于活动状态
    state_single 控件包含多个子控件时,确定是否只显示一个子控件
    state_first 控件包含多个子控件时,确定第一个子控件是否处于显示状态
    state_middle 控件包含多个子控件时,确定中间一个子控件是否处于显示状态
    state_last 控件包含多个子控件时,确定最后一个子控件是否处于显示状态

    定义案例:

    btn_bg1.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@color/color1" android:state_pressed="true"/>
        <item android:drawable="@color/color4" android:state_enabled="false"/>
        <item android:drawable="@color/color3" />
    </selector>

    layout_btn.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingTop="50dp">
        <Button
            android:id="@+id/btnOne"
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:background="@drawable/btn_bg1"
            android:text="按钮"
            android:textColor="#ffffff"
            android:textSize="20sp"
            android:textStyle="bold" />
        <Button
            android:id="@+id/btnTwo"
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:text="按钮不可用"
            android:textColor="#000000"
            android:textSize="20sp"
            android:textStyle="bold" />
    </LinearLayout>
    

    MainActivity.java

    public class MainActivity extends Activity {
        private Button btnOne,btnTwo;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btnOne = (Button) findViewById(R.id.btnOne);
            btnTwo = (Button) findViewById(R.id.btnTwo);
            btnTwo.setOnClickListener(new OnClickListener() { //按钮绑定点击
                事件
                @Override
                public void onClick(View v) {
                    if(btnTwo.getText().toString().equals("按钮不可用")){
                        btnOne.setEnabled(false);
                        btnTwo.setText("按钮可用");
                    }else{
                        btnOne.setEnabled(true);
                        btnTwo.setText("按钮不可用");
                    }
                }
            });
        }
    }
    
  • 相关阅读:
    50 个 jQuery 插件可将你的网站带到另外一个高度
    查看占用IO的进程
    google全球地址
    php http请求封装
    如何促使团队紧密协作
    pureftpd.passwd解析
    goaccess生成nginx每日访问纪录
    bootstrap的一些资源
    php write_ini_file
    mysql查询区分大小写
  • 原文地址:https://www.cnblogs.com/ziyue7575/p/a8e2ad5a204512d9cce7f20f6f3a255e.html
Copyright © 2020-2023  润新知