Android中Shape、selector在xml中的使用、切换按钮的背景图案
来源:互联网 发布:java面向对象练习题 编辑:程序博客网 时间:2024/05/18 01:27
如果你想让背景有边界,使用下面的代码:在res/drawable下新建buttonshape.xml
一、Android中Shape使用
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/添加的图片" /> <item > <shape> <solid/> <corners android:radius="100dp"/><sizeandroid:width="180dp"android:height="180dp"/> <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" /> </shape> </item> </layer-list>
在layout中加背景
<Buttonandroid:id="@+id/button"android:layout_centerInParent="true"android:layout_width="180dp"android:layout_height="180dp"android:background="@drawable/buttonshape"/>
二、接下来是点击按钮改变按钮背景图案,焦点一移开,图案又恢复原貌。(有2个方法可以实现,一种是用 选择器 selector)
<?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/earth1" /> //表示按钮View处于按下状态的图片 <item android:state_focused="true" android:drawable="@drawable/earth1" />//表示按钮View获取焦点时的图片 <item android:drawable="@drawable/earth0" />//表示View默认的图片</selector>
<Buttonandroid:id="@+id/button"android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content"android:background="@drawable/button_selector"/>
相关属性:
android:state_selected是选中
android:state_focused是获得焦点
android:state_pressed是点击
android:state_enabled是设置是否响应事件,指所有事件
另一种就是在触摸事件中直接设置
private Button mBtn; //定义我们的按钮
在onCreate中加入
mBtn = (Button) findViewById(R.id.btn); //btn为layout中的Button ID
mBtn.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View arg0,MotionEvent arg1)
{
if(arg1.getAction() == MotionEvent.ACTION_DOWN)
{
arg0.setBackgroundResource(R.drawable.pressed); //按下的图片对应pressed
}
else if(arg1.getAction() == MotionEvent.ACTION_UP)
{
arg0.setBackgroundResource(R.drawable.normal); //常态下的图片对应normal
}
else if() //这里还可以继续实现MotionEvent.ACTION_MOVE和MotionEvent.ACTION_CANCEL等实现更多的特效
return false;
}
});
三、通过按钮切换button的背景图案
public class MainActivity extends Activity implements OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button=(Button)findViewById(R.id.button); button.setOnClickListener(this); } int i=0; @Override public void onClick(View v){ i++; switch(i){ case 1: v.findViewById(R.id.button).setBackgroundResource(R.drawable.earth1); Toast.makeText(MainActivity.this,"是个a",Toast.LENGTH_SHORT).show(); break; case 2: v.findViewById(R.id.button).setBackgroundResource(R.drawable.earth0); Toast.makeText(MainActivity.this,"是个b",Toast.LENGTH_SHORT).show(); break; case 3: v.findViewById(R.id.button).setBackgroundResource(R.drawable.earth1); Toast.makeText(MainActivity.this,"是个c",Toast.LENGTH_SHORT).show(); i=1; break; default: break; } } }
0 0
- Android中Shape、selector在xml中的使用、切换按钮的背景图案
- Android中Shape 在xml中的使用
- Android XML 文件 中 的 selector、shape
- Android中 shape和selector的使用
- android中shape与selector的使用
- Android中Shape和selector的使用
- Android中shape和selector的使用
- android中selector中的shape
- android selector中使用shape
- Android中Shape Drawable在xml中的使用
- Android中Shape Drawable在xml中的使用
- 使用Android中的shape,来替代使用图片做按钮的背景。
- Android shape、selector的使用
- 按钮背景效果切换 selector
- Android中的Shape,selector使用总结
- Android中的selector&&shape
- Android中Shape和Selector的结合使用。
- Android中 selector 和 shape 的使用笔记
- 学习使用OpenSSL生成并使用RSA密钥对
- My97DatePicker参数配置方法大全
- hibernate映射(三)——一对一
- 统计ip,进行合并输出
- Android UI---View与ViewGroup
- Android中Shape、selector在xml中的使用、切换按钮的背景图案
- 使用OpenH323开发
- 通过Volley我们能学到什么?(2) — 刨析网络请求框架
- Git入门基础 - CSDN学院视频
- jsp取出session的几种方式
- java 对List集合进行排序
- 素数判断和素数筛法
- linux命令
- GBK,UTF-8,和ISO8859-1编码区别与get,post请求中文乱码处理