【发布时间】:2014-09-02 21:57:42
【问题描述】:
我为我的布局制作了一个自定义按钮,但问题是它没有显示 onclick 效果。 这是我构建的三个 xml 文件 我给了一个椭圆形的按钮你能告诉我如何把按钮做成一个圆形并且尺寸更小吗? 提前谢谢
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0"
android:gravity="center"
android:textSize="25sp" />
<Spinner
android:id="@+id/spin"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"
android:text="heyyy"
android:layout_below="@+id/textView1"
/>
<Button
android:layout_marginTop="20dp"
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="150sp"
android:text="add"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_below="@+id/spin"
android:background="@drawable/button_xml"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/button1"
android:gravity="left"
android:textSize="25sp"
android:text="add" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:gravity="right"
android:textSize="25sp"
android:text="a" />
<Button
android:layout_marginTop="20dp"
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="50sp"
android:text="reset"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_below="@+id/textView3"/>
</RelativeLayout>
</ScrollView>
button_xml.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
>
<solid
android:color="#03A89E"/>
<stroke android:width="2dp"
android:color="#ffffff"/>
</shape>
button_click.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/button_xml"
/>
</selector>
【问题讨论】:
-
将默认状态drawable添加到您的选择器项并将按钮背景从android:background="@drawable/button_xml"更改为android:background="@drawable/button_click"