Button

    我们可以设置Button文字或者有图的,有图可以适应ImageButton,当然也可以使用 background属性,或者官网的drawableLeft

属性。设置图片的样式还是需要慢慢搞的。

   然后,响应点击事件,首先可以设置onClick方法,在view所在的Activity中声明同名的方法,这个方法必须是publi,void,

而且必须要有一个View参数,这个系统会自动传过去吧,不过你要是不设置,会报错,自己就报错了。

    如果在运行时实例一个Button,尤其是在Fragment的子类中,可以创建一个View.OnClickListener,传进去

设置监听,监听点击事件,不用写死了用哪个方法

Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // Do something in response to button click
    }
});

最后是设置button的样式,可以通过style或者Themes,就是自定义style文件,

这个stype可以继承,在res/values/,可以覆盖父类的属性,这样可以方便的定义一类内容,还是可以的。

其他的定义Themes还是需要看看。


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CodeFont" parent="@android:style/TextAppearance.Medium">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#00FF00</item>
        <item name="android:typeface">monospace</item>
    </style>
     
    <style name="CodeFont.Red">
        <item name="android:textColor">#FF0000</item>
    </style>

    <style name="CodeFont.Red.Big">
        <item name="android:textSize">30sp</item>
    </style>

</resources>

还有就是自定义背景了,写一个xml,在res/drawable/


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_pressed"
          android:state_pressed="true" />
    <item android:drawable="@drawable/button_focused"
          android:state_focused="true" />
    <item android:drawable="@drawable/button_default" />
</selector>

然后使用background设置就好了。



你可能感兴趣的:(Button)