Android基础篇(二)

StateListDrawable

针对不同的视图状态加载不同的图片资源来显示的资源选择器

    android:state_pressed   按下
    android:state_selected  选中(检点状态)
    android:state_checked   选择框的选择状态
    android:stata_enable    可用状态

使用方法:

        可以在drawable下定义跟标签为selector的xml文件
        以选择框的状态为例,文件名为check_button_drawable.xml;
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--  未选中状态 -->
<item android:drawable="@drawable/button_un_checkedl" android:state_checked="false"/>
<!--  选中状态 -->
<item android:drawable="@drawable/button_checked" android:state_checked="true"/>
</selector>

布局中直接使用 @drawable来加载选择器即可

视图的基本状态(以按钮为例)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--  可用未按下 -->
<item android:drawable="@drawable/btn_nomal" android:state_enabled="true" android:state_pressed="false"/>
<!--  可用按下 -->
<item android:drawable="@drawable/btn_press" android:state_enabled="true" android:state_pressed="true"/>
<!--  不可用 -->
<item android:drawable="@drawable/btn_no_enable" android:state_enabled="false"/>
</selector>
使用(设置到背景)
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 点击有惊喜 "
android:background="@drawable/btn_bg_drawable" /

针对视图的可用状态在xml中可以使用android:enable属性

    android:enabled="false" false 表示不可用,默认 true 表示可用
    该属性在 Java 对应的是
    btn.setEnable(true) btn.setEnable(false);
    判断是否可用
    btn.isEnable();

ColorStateListDrawable

颜色选择器,可以根据不同状态使用不同的颜色值,需要在 res 下新建一个 color 文件夹,然后在 color 文件夹中新建 xml 文件,根节点为 selector ,以按钮上的
文本为例,新建一个文件: tx_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--  未按下 -->
<item android:state_enabled="true" android:state_pressed="false" android:color="#000000"/>
<!--  按下 -->
<item android:state_enabled="true" android:state_pressed="true" android:color="@color/white"/>
<!--  不可用 -->
<item android:state_enabled="false" android:color="#666666"/>
</selector>

注意:如果针对视图的背景 ( 也就是需要用图像、图像来显示的地方 ) 使用颜色,则颜色选择的定义属于 drawable ,定义方式需要在 drawable 中定义选择器

shape

可以通过定义 shape 实现形状的显示,一般放在 drawable 文件夹中,跟标签为 shape

shape 属性表示形状类型,默认 rectangle 矩形, oval 椭圆, line 线条, ring 圆环
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!--  渐变填充 , 跟 solid 二选一 -->
<!--
<gradient
android:endColor="#EE941A" 起始颜色
android:centerColor="#ff0000" 中间颜色
android:startColor="#F6E5AE" 结束颜色
android:type="sweep" 类型,默认 linea 线性, radial 放射, sweep 扫描
android:angle="90" 角度(只对 linear 类型有效 0 从左到右, 90 从上到下)
android:centerX="50%" 中心 ( 针对放射以及扫描 )x 坐标
android:centerY="50%" /> 中心 y 坐标
-->
<!--  圆角 ( 针对矩形 ) -->
<corners
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp" />
<!--  纯色填充,跟 gradient 二选一 -->
<solid android:color="#ff0000" />
<!--  边框 -->
<stroke 如果是虚线需要添加 dashGapdashWidth 两个属性
android:dashGap="5dp" 虚线之间短线间距
android:dashWidth="15dp" 虚线短线长度
android:width="2dp"
android:color="#0000ff" />
<!--  内容边距 -->
<padding
android:bottom="3dp"
android:left="5dp"
android:right="5dp"
android:top="3dp" />
</shape>

形状一般用来做背景
如果一个视图中不同状态有不同形状,可以在 selector 的 item 中单独定义,也可以直接用 @drawable/xxx 方式加载

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="false">
        <shape>
            <corners android:radius="10dp"/>
            <solid android:color="#ff0000"/>
        </shape>
    </item>
    <item android:state_pressed="true">
        <shape>
            <corners android:radius="10dp"/>
            <solid android:color="#ffff00"/>
        </shape>
    </item>
</selector>

Activity之间的跳转

1、定义Activity类(定义的类继承Activity 或者其子类,设置布局 )

    public class LoginActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //  设置界面布局
            setContentView(R.layout.activity_login);
        }
    }

2 、在 manifest.xml 中注册 Activity

<activity
android:name="com.xykj.day5.LoginActivity"
android:label="@string/login_activity_label" >
</activity>

3 、创建 Intent 对象,使用 startActivity 来启动

// 创建一个意图
// 第一个参数是一个 Context 对象 (Activity\Service\Application)
// 第二个参数表示意图要关联的目标,使用的是 Class 实例
Intent it = new Intent(MainActivity.this,LoginActivity.class);
// 启动窗口
startActivity(it);

猜你喜欢

转载自blog.csdn.net/weixin_42097095/article/details/80587458