zoukankan      html  css  js  c++  java
  • 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("按钮不可用");
                    }
                }
            });
        }
    }
    
  • 相关阅读:
    [C/C++] 指针数组和数组指针
    [计算机网络] DNS劫持和DNS污染
    [计算机网络-数据链路层] CSMA、CSMA/CA、CSMA/CD详解
    [BinaryTree] 二叉树常考知识点
    NODE-windows 下安装nodejs及其配置环境
    MATLAB/Excel-如何将Excel数据导入MATLAB中
    Excel-怎样实现行列转置
    一篇文章学懂Shell脚本
    SQL-MySQL使用教程-对MySQL的初步尝试
    资源贴-在线编译环境推荐
  • 原文地址:https://www.cnblogs.com/ziyue7575/p/a8e2ad5a204512d9cce7f20f6f3a255e.html
Copyright © 2011-2022 走看看