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