展会信息港展会大全

Android自定义Button按钮
来源:互联网   发布日期:2015-12-07 15:28:52   浏览:1721次  

导读:Android自定义Button按钮主要可以分成两种形式:1.通过自定MyButton类来继承Button,将所有效果在类中实现.2.通过xml文件来改变Button的样式和颜色.今天我...

Android自定义Button按钮主要可以分成两种形式:

1.通过自定MyButton类来继承Button,将所有效果在类中实现.

2.通过xml文件来改变Button的样式和颜色.

今天我就先讲通过xml文件,稍后封装自定义Button类再补上.

TestcActivity

[html]

package com.example.blueapp;

import android.app.Activity;

import android.os.Bundle;

public class TestcActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.testc);

}

}

package com.example.blueapp;

import android.app.Activity;

import android.os.Bundle;

public class TestcActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.testc);

}

}

testc.xml

[html]

<?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" >

<Button

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="20dp"

android:text="测试按钮"

android:background="@drawable/button_style"

/>

<Button

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="20dp"

android:text="原始按钮"

/>

</LinearLayout>

<?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" >

<Button

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="20dp"

android:text="测试按钮"

android:background="@drawable/button_style"

/>

<Button

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="20dp"

android:text="原始按钮"

/>

</LinearLayout>

button_style.xml

[html]

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true">

<shape>

<gradient android:startColor="#0d76e1" android:endColor="#0d76e1"

android:angle="270" />

<stroke android:width="1dip" android:color="#f403c9" />

<corners android:radius="2dp" />

<padding android:left="10dp" android:top="10dp"

android:right="10dp" android:bottom="10dp" />

</shape>

</item>

<item android:state_focused="true">

<shape>

<gradient android:startColor="#ffc2b7" android:endColor="#ffc2b7"

android:angle="270" />

<stroke android:width="1dip" android:color="#f403c9" />

<corners android:radius="2dp" />

<padding android:left="10dp" android:top="10dp"

android:right="10dp" android:bottom="10dp" />

</shape>

</item>

<item>

<shape>

<gradient android:startColor="#000000" android:endColor="#ffffff"

android:angle="180" />

<stroke android:width="1dip" android:color="#f403c9" />

<corners android:radius="5dip" />

<padding android:left="10dp" android:top="10dp"

android:right="10dp" android:bottom="10dp" />

</shape>

</item>

</selector>

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true">

<shape>

<gradient android:startColor="#0d76e1" android:endColor="#0d76e1"

android:angle="270" />

<stroke android:width="1dip" android:color="#f403c9" />

<corners android:radius="2dp" />

<padding android:left="10dp" android:top="10dp"

android:right="10dp" android:bottom="10dp" />

</shape>

</item>

<item android:state_focused="true">

<shape>

<gradient android:startColor="#ffc2b7" android:endColor="#ffc2b7"

android:angle="270" />

<stroke android:width="1dip" android:color="#f403c9" />

<corners android:radius="2dp" />

<padding android:left="10dp" android:top="10dp"

android:right="10dp" android:bottom="10dp" />

</shape>

</item>

<item>

<shape>

<gradient android:startColor="#000000" android:endColor="#ffffff"

android:angle="180" />

<stroke android:width="1dip" android:color="#f403c9" />

<corners android:radius="5dip" />

<padding android:left="10dp" android:top="10dp"

android:right="10dp" android:bottom="10dp" />

</shape>

</item>

</selector>

gradient 主体渐变 startColor开始颜色,endColor结束颜色 ,angle开始渐变的角度(值只能为90的倍数,0时为左到右渐变,90时为下到上渐变,依次逆时针类推)

stroke 边框 width 边框宽度,color 边框颜色

corners 圆角 radius 半径,0为直角

padding text值的相对位置

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港