android selector背景选择器,按下有不同效果
来源:互联网 发布:岭南大学 知乎 编辑:程序博客网 时间:2024/05/22 14:08
方法一:代码实现
1. 自定义状态效果可以通过代码实现,也可以通过xml定义style实现。
2. 下面先介绍代码实现,通过StateListDrawable定义Button背景。
3. 由于View类中PRESSED_ENABLED_STATE_SET值不是公共常量,所以通过继承来访问了。
特注:其他控件的效果,比如ImageView,也可以通过这种方法实现,但是由于ImageView默认是没焦点,不可点击的,需要自己更改(需要点击就设置android:clickable="true" , 需要能够选中就设置android:focusable="true" )。
java 代码:
01
package
com.test.TestButton;
02
03
import
android.app.Activity;
04
import
android.content.Context;
05
import
android.graphics.drawable.Drawable;
06
import
android.graphics.drawable.StateListDrawable;
07
import
android.os.Bundle;
08
import
android.view.View;
09
import
android.widget.Button;
10
11
public
class
TestButton
extends
Activity {
12
@Override
13
public
void
onCreate(Bundle savedInstanceState) {
14
super
.onCreate(savedInstanceState);
15
setContentView(R.layout.main);
16
Integer[] mButtonState = { R.drawable.defaultbutton,
17
R.drawable.focusedpressed, R.drawable.pressed };
18
Button mButton = (Button) findViewById(R.id.button);
19
MyButton myButton =
new
MyButton(
this
);
20
mButton.setBackgroundDrawable(myButton.setbg(mButtonState));
21
}
22
23
class
MyButton
extends
View {
24
25
public
MyButton(Context context) {
26
super
(context);
27
}
28
29
// 以下这个方法也可以把你的图片数组传过来,以StateListDrawable来设置图片状态,来表现button的各中状态。未选
30
// 中,按下,选中效果。
31
public
StateListDrawable setbg(Integer[] mImageIds) {
32
StateListDrawable bg =
new
StateListDrawable();
33
Drawable normal =
this
.getResources().getDrawable(mImageIds[
0
]);
34
Drawable selected =
this
.getResources().getDrawable(mImageIds[
1
]);
35
Drawable pressed =
this
.getResources().getDrawable(mImageIds[
2
]);
36
bg.addState(View.PRESSED_ENABLED_STATE_SET, pressed);
37
bg.addState(View.ENABLED_FOCUSED_STATE_SET, selected);
38
bg.addState(View.ENABLED_STATE_SET, normal);
39
bg.addState(View.FOCUSED_STATE_SET, selected);
40
bg.addState(View.EMPTY_STATE_SET, normal);
41
return
bg;
42
}
43
}
44
}
XML代码:
在res/drawable下面新建mybutton_background.xml文件,内容如下:
01
<?
xml
version=”1.0″ encoding=”utf-8″?>
02
<
selector
xmlns:android=”http://schemas.android.com/apk/res/android“>
03
<
item
android:state_focused=”true”
04
android:state_pressed=”false”
05
android:drawable=”@drawable/yellow” />
06
<
item
android:state_focused=”true”
07
android:state_pressed=”true”
08
android:drawable=”@drawable/green” />
09
<
item
android:state_focused=”false”
10
android:state_pressed=”true”
11
android:drawable=”@drawable/blue” />
12
<
item
android:drawable=”@drawable/grey” />
13
</
selector
>
这里面就定义了在不同状态下的显示图片,然后在layout里面定义Button的时候,指定它的background为这个mybutton_background
01
<?
xml
version=”1.0″ encoding=”utf-8″?>
02
<
LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
03
android:orientation=”vertical”
04
android:layout_width=”fill_parent”
05
android:layout_height=”fill_parent”
06
>
07
<
Button
android:id=”@+id/btn”
08
android:layout_width=”wrap_content”
09
android:layout_height=”wrap_content”
10
android:text=”@string/mybtn”
11
android:background=”@drawable/mybutton_background” />
12
</
LinearLayout
>
这种方式开发比较简单,适合做一些风格一致的Button,设置成同一个background就可以了。ImageView等控件如方法一中所述。
- android selector背景选择器,按下有不同效果
- selector背景选择器不同效果
- android selector 背景选择器
- android背景选择器selector
- android selector背景选择器
- android 背景选择器selector
- android背景选择器selector
- Android-selector背景选择器
- Android背景选择器selector
- android selector背景选择器
- Android Selector 背景选择器
- 【Android 界面效果4】android背景选择器selector用法汇总
- android的selector,背景选择器
- android的selector,背景选择器
- Android的selector,背景选择器
- Android 的 selector--背景选择器
- Android的selector,背景选择器
- android背景选择器selector用法
- ubuntu下安装飞鸽传书
- sencha 2.0 的tabpanel中使用formpanel,carousel 等的用法
- C++学习笔记(十) 内存机制与Allocator
- A*算法需要注意的问题
- hdu 1507 Uncle Tom's Inherited Land*
- android selector背景选择器,按下有不同效果
- 科研选题与写作投稿
- 根据ip地址判断国家
- VC++实现浏览器自动填表
- Memory Hierarchy-计算机各级存储器速度对比
- [转]毕业五年决定你的命运-----值得所有不甘平庸的人看看
- 自定义Android图片上加文字组件
- jbpm4.4: not delete: [org.jbpm.pvm.internal.model.ExecutionImpl#8]
- 虚拟机安装Ubuntu的上网设置(有线网络和无线网络)