首页 > 移动平台 > 详细

android开发最常用例子整理----(1)自定义按钮实现

时间:2015-01-19 00:16:26      阅读:242      评论:0      收藏:0      [点我收藏+]

android开发最常用例子整理----(1)自定义按钮实现


一、Activity

MainActivity.java源码:

技术分享

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	
	
}


二、xml布局文件

activity_main.xml源码:

技术分享

<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="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_button"/>
</LinearLayout>


三、相关资源

res/drawable/下的文件:

技术分享

bg_button.xml源码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_focused="true" android:drawable="@drawable/bg_button_pressed"></item>
	<item android:state_pressed="true" android:drawable="@drawable/bg_button_pressed"></item>
	<item  android:drawable="@drawable/bg_button_normal"></item>
</selector>
补充:这里除了可以设置state_focused、state_pressed状态下的样式外,还可以设置state_checked、state_selected等状态。
bg_button_normal.png:技术分享

bg_button_pressed.png:技术分享


四、效果截图

(1)自然状态下的按钮:

技术分享

(2)按压状态下的按钮:

技术分享




android开发最常用例子整理----(1)自定义按钮实现

原文:http://blog.csdn.net/wen294299195/article/details/42847555

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!