我有一个应用程序,我必须制作圆形按钮,我成功制作但我想要的时候我点击按钮然后更改背景drawable但当我这样做圆形按钮变得不可见。我该怎么做
代码: -
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<stroke android:color="@color/colorPrimary" android:width="5dp" />
<solid android:color="@color/colorPrimaryDark"/>
<size android:width="150dp" android:height="150dp"/>
</shape>
</item>
答案 0 :(得分:1)
使用代码
更改图像public void onClick(View v) {
if(v == ButtonName) {
ButtonName.setImageResource(R.drawable.ImageName);
}
}
或者,使用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/login_selected" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/login_mouse_over" /> <!-- focused -->
<item android:drawable="@drawable/login" /> <!-- default -->
</selector>
在OnClick中,只需添加以下代码:
ButtonName.setBackgroundDrawable(getResources().getDrawable(R.drawable.ImageName));
您可以使用此drawable获取desired circle
UPD :setBackgroundDrawable
已弃用,请查看here。
答案 1 :(得分:1)
使用它 - drawable/style_circular_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape android:shape="oval">
<solid android:color="@color/colorDeepOrange"/>
<size android:width="120dp" android:height="120dp"/>
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="oval">
<solid android:color="@color/colorOrange"/>
<size android:width="120dp" android:height="120dp"/>
</shape>
</item>
<item >
<shape android:shape="oval">
<solid android:color="@color/colorOrange"/>
<size android:width="120dp" android:height="120dp"/>
</shape>
</item>
</selector>
并在style_circular_button.xml
-
Button
<Button
android:id="@+id/btnSignin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_btn_login"
android:background="@drawable/style_circular_button"/>