简体   繁体   English

使用imange作为背景时,将圆角设置为按钮--Android Studio

[英]Setting rounded corners to an an button while using an imange as the background - Android Studio

I am trying to get rounded corners on my button. 我想在我的按钮上找到圆角。 I am using a selector xml to define different button states but the grass.png image seems to be overriding other attributes. 我使用选择器xml来定义不同的按钮状态,但grass.png图像似乎覆盖了其他属性。

My resulting button is just square. 我的结果按钮就是方形。

Any suggestions on how to have a button with rounded corners while using a .png as the background? 有关如何使用.png作为背景的圆角按钮的任何建议?

selector_button.xml 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>

Button style: 按钮样式:

<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 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"/>

------EDIT------------ Attempting Trevor Harts solution. ------编辑------------尝试Trevor Harts解决方案。

The background did have rounded buttons but my grass image doesn't fill the button and there is no longer any text on the button. 背景确实有圆形按钮,但我的草图像没有填满按钮,按钮上不再有任何文字。

button_default.xml 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 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" />

Do something like this: 做这样的事情:

RoundedButton.xml: RoundedButton.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/Green" />
  <corners 
      android:bottomRightRadius="5dp"
      android:bottomLeftRadius="5dp"
      android:topRightRadius="5dp"
      android:topLeftRadius="5dp"/>
</shape>

Your layout: 你的布局:

<ImageButton
            android:background="@drawable/RoundedButton"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:src="@drawable/buttonImage"
            android:layout_width="78.0dp"
            android:layout_height="78.0dp"
            android:id="@+id/myButton"
            android:layout_centerHorizontal="true"
            android:layout_row="0"
            android:layout_columnSpan="1"
            android:layout_column="3"
            android:layout_gravity="center" />

If you need to programmatically do this, you can do so like this: 如果您需要以编程方式执行此操作,您可以这样做:

        Button btn = new Button(this);
        btn.SetBackgroundResource(Resource.Drawable.RoundedButton);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM