【发布时间】:2017-10-14 15:52:49
【问题描述】:
我试图在我的按钮上获得圆角。 我正在使用选择器 xml 来定义不同的按钮状态,但 Grass.png 图像似乎覆盖了其他属性。
我生成的按钮是方形的。
关于如何在使用 .png 作为背景时使用圆角按钮有什么建议吗?
selector_button.xml
<?xml version="1.0" encoding="utf-8"?>
<item android:state_pressed="true" android:drawable="@color/colorAccent">
<shape android:shape="rectangle" >
<corners android:radius="10dp" />
</shape>
</item>
<item android:state_focused="true" android:drawable="@drawable/grass">
<shape android:shape="rectangle" >
<corners android:radius="10dp" />
<stroke android:color="@color/colorAccent"/>
</shape>
</item>
<item android:drawable="@drawable/grass">
<shape android:shape="rectangle" >
<corners android:radius="10dp" />
<stroke android:color="@color/colorAccent"/>
</shape>
</item>
按钮样式:
<style name="button">
<item name="android:textColor">@color/buttonText</item>
<item name="android:textAllCaps">false</item>
<item name="android:textSize">18sp</item>
<item name="android:background">@drawable/selector_button</item>
</style>
layout.xml
<Button
android:id="@+id/newGame_button"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="@string/newGame_button"
android:textSize="30sp"
android:layout_marginStart="25dp"
android:layout_marginEnd="25dp"
android:onClick="NewGameClicked"
style="@style/button"/>
--------编辑------------ 正在尝试 Trevor Harts 解决方案。
背景确实有圆形按钮,但我的草图像没有填满按钮,按钮上不再有任何文字。
button_default.xml
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/colorAccent" />
<corners
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:topRightRadius="5dp"
android:topLeftRadius="5dp"/>
</shape>
activity_main.xml
<ImageButton
android:id="@+id/newGame_button"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="@string/newGame_button"
android:textSize="30sp"
android:layout_marginStart="25dp"
android:layout_marginEnd="25dp"
android:onClick="NewGameClicked"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:background="@drawable/button_default"
android:src="@drawable/grass" />
【问题讨论】:
-
我推荐的方法有用吗?
-
我没有工作。按钮仍然是方形的。
-
您可以将您的代码更新为您所做的吗?