Android实例-手机安全卫士(二十)-使用shape形状,自定义组件边框、形状等

一、目标。

  1、实现文本组件的点击事件。

  2、实现组件(TextView等)以形状(圆角矩形、椭圆、圆环等)显示,也可为其增加自定义边框、渐变等属性。并可以与样式组合,可实现默认、按下等过程显示不同的形状。

 默认:  按下:

二、代码实现。

  1、TextView对象带有点击事件,需对clickable属性设为true,并取名点击事件名称,即onclick属性值为resetWizard。

  2、在手机防盗界面实现点击事件处理,(即重新进入”设置向导“界面)

点击事件处理代码;

//重新进入防盗设置向导界面
	public void resetWizard(View view){
		Intent intent =new Intent(SecurityActivity.this,SetupWizard_ui_1.class);
		startActivity(intent);	
		finish();
	} 

  2、shape形状。(可参照“参考文档介绍)

    ①.在res文件夹下的drawable文件夹内新建文件(new-file),取名(gradient_box)并以.xml为后缀。

    ②.在新建文件中确定xml版本和编码格式。再建一个<shape>标签,在其属性中确定命名空间(xmlns:android="http://schemas.android.com/apk/res/android"

android:shape="形状"),形状的值可以指定需要的形状(矩形rectangle、椭圆形oval、直线line、环形ring)

    ③.在<shape>标签中增加其他标签用于指定形状的样式(如角corners、直径、渐变gradient等),在这些样式标签的属性中指定相应的值。

shape代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners android:radius="15dip" />
	<!-- 渐变 -->
    <gradient
        android:endColor="#0000ff"
        android:startColor="#ff0000" />
    <!-- 固定颜色,与渐变冲突 -->
    
    <solid android:color="#50000000"/>
    <!-- 边框 -->
    <stroke android:width="2dip"
        android:color="#000000"
        android:dashWidth="3dip"
        android:dashGap="5dip"/>

</shape>

  3、在需要使用shape形状的组件中设置background属性,通过@drawable找到新建的样式文件。

  4、出background属性可引用外,在drawable文件夹下的style.xml中也可以引用该样式,从而实现默认、聚焦、按下等不同过程显示不同的形状样式。

新建样式代码:

<?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/gradient_box_pressed" ></item>  <!-- press -->
    
    <item android:state_enabled="true"
        android:drawable="@drawable/btn_default_selected" /> <!-- focused -->   

    <item android:drawable="@drawable/gradient_box"/> <!-- default -->   

</selector>

 

posted @ 2015-01-28 16:29  红烧大白鲨  阅读(405)  评论(0编辑  收藏  举报