`
zcwfeng
  • 浏览: 97749 次
  • 性别: Icon_minigender_1
  • 来自: 吉林
社区版块
存档分类
最新评论

android Button Theme 自定义小技巧

 
阅读更多

方法一

1.放在drawable下的selector.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/temp1" />
<item android:state_pressed="false" android:state_focused="false"
android:drawable="@drawable/temp2" />
<item android:state_focused="true" android:drawable="@drawable/temp3" />
<item android:state_focused="false" android:drawable="@drawable/temp4" />
</selector>

2.布局文件main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:drawableTop="@drawable/shouru"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:background="@drawable/selector"/>
</LinearLayout>

3.只是为了测试,所以效果不是很好,Button部分状态效果图如下:

①初始化的时候默认显示的按钮效果:

②点击后释放显示的效果

③点击不放时的效果

方法二

1.布局文件main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/button"
android:drawableTop="@drawable/shouru"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:background="@drawable/temp4"/>
</LinearLayout>

2.主要的java代码,实现点击效果:
Button button = (Button) this.findViewById(R.id.button);
button.setOnTouchListener(new Button.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
v.setBackgroundResource(R.drawable.temp1);
Log.i("TestAndroid Button", "MotionEvent.ACTION_DOWN");
}
else if(event.getAction() == MotionEvent.ACTION_UP){
v.setBackgroundResource(R.drawable.temp2);
Log.i("TestAndroid Button", "MotionEvent.ACTION_UP");
}
return false;
}
});



分享到:
评论

相关推荐

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    3.21 Android变脸——主题(Theme)实现 第4章 史上超豪华的手机控件 4.1 EditText与TextView共舞——setOnKeyListener事件 4.2 设计具有背景图的按钮——ImageButton的焦点及事件处理 4.3 给耶诞老人的信息——Toast...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    3.21 Android变脸——主题(Theme)实现 第4章 史上超豪华的手机控件 4.1 EditText与TextView共舞——setOnKeyListener事件 4.2 设计具有背景图的按钮——ImageButton的焦点及事件处理 4.3 给耶诞老人的信息——Toast...

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    3.21 Android变脸——主题(Theme)实现 第4章 史上超豪华的手机控件 4.1 EditText与TextView共舞——setOnKeyListener事件 4.2 设计具有背景图的按钮——ImageButton的焦点及事件处理 4.3 给耶诞老人的信息——Toast...

    Google Android SDK开发范例大全的目录

    3.21 Android变脸——主题(Theme)实现 第4章 史上超豪华的手机控件 4.1 EditText与TextView共舞——setOnKeyListener事件 4.2 设计具有背景图的按钮——ImageButton的焦点及事件处理 4.3 给耶诞老人的信息——Toast...

    Google Android SDK开发范例大全(完整版附部分源码).pdf

    3.21 Android变脸——主题(Theme)实现 第4章 史上超豪华的手机控件 4.1 EditText与TextView共舞——setOnKeyListener事件 4.2 设计具有背景图的按钮——ImageButton的焦点及事件处理 4.3 给耶诞老人的信息——...

    Google Android SDK 开发范例大全01

    3.21 Android变脸——主题(Theme)实现 第4章 史上超豪华的手机控件 4.1 EditText与TextView共舞——setOnKeyListener事件 4.2 设计具有背景图的按钮——ImageButton的焦点及事件处理 4.3 给耶诞老人的信息——Toast...

    Google Android SDK 开发范例大全02

    3.21 Android变脸——主题(Theme)实现 第4章 史上超豪华的手机控件 4.1 EditText与TextView共舞——setOnKeyListener事件 4.2 设计具有背景图的按钮——ImageButton的焦点及事件处理 4.3 给耶诞老人的信息——Toast...

    Google+Android+SDK开发范例大全

    ——具选择功能的对话框 3.21 Android变脸——主题(Theme)实现 第4章 史上超豪华的手机控件 4.1 EditText与TextView共舞——setOnKeyListener事件 4.2 设计具有背景图的按钮——ImageButton的焦点及事件处理 4.3 给...

    Google Android sdk 开发范例大全 部分章节代码

    3.21 Android变脸——主题(Theme)实现 第4章 史上超豪华的手机控件 4.1 EditText与TextView共舞——setOnKeyListener事件 4.2 设计具有背景图的按钮——ImageButton的焦点及事件处理 4.3 给耶诞老人的信息——Toast...

    Google Android SDK开发范例大全(完整版)

    3.21 Android变脸——主题(Theme)实现 第4章 史上超豪华的手机控件 4.1 EditText与TextView共舞——setOnKeyListener事件 4.2 设计具有背景图的按钮——ImageButton的焦点及事件处理 4.3 给耶诞老人的信息——Toast...

Global site tag (gtag.js) - Google Analytics