如何在Android中制作带有圆角、背景图像和文本的按钮

3

我想创建一个Android按钮,其文本可以更改(以便轻松地国际化应用程序),但同时具有背景,并且如果可能,则使用圆角。

到目前为止,如果我使用背景图像,我不能使角落变圆,反之亦然。

我尝试的最后一件事是创建这些XML文件

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <stroke android:width="2dp" android:color="#FF404040" />
    <corners android:radius="6dp" />
    <gradient android:startColor="#FF6800" android:centerColor="#FF8000" android:endColor="#FF9700" android:angle="90" />
</shape>

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <stroke android:width="2dp" android:color="#FF404040" />
    <corners android:radius="6dp" />
    <gradient android:startColor="#FF6800" android:centerColor="#FF8000" android:endColor="#FF9700" android:angle="90" />
</shape>

我可以添加按钮,但是我无法添加背景图片:

 <Button
        android:id="@+id/button"
        android:layout_width="235dp"
        android:layout_height="wrap_content"
        android:background="@drawable/background_selector"
        android:text="Button"
        tools:layout_editor_absoluteX="101dp"
        tools:layout_editor_absoluteY="277dp" />

我想实现的效果类似于这个应用程序中的“Morning Ritual”按钮,例如,如果那是一个按钮,我也不知道。请参考此链接:https://istack.dev59.com/mdxRR.webp

很可能是一个卡片视图,请查看此链接 https://material.io/design/components/cards.html# - sanoJ
3个回答

2
您可以使用ImageButton,其中android:src是您需要的图像,android:background是圆形形状。"最初的回答"
<ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/roundedImageButton"
            android:src="@drawable/backgound"
            android:text="TEXT" 
            android:background="@drawable/roundedShape" />

最初的回答:或者使用CardView:
<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="5dp"> 

     <ImageView
        android:src="@drawable/background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY" />

     <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="TEXT"/>

   </androidx.cardview.widget.CardView>

2
你可以使用 CardView,如果你想创建一个 Button,可以尝试使用以下的xml drawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="rectangle" android:padding="5dp">
            <corners
                 android:bottomRightRadius="4dp"
                 android:bottomLeftRadius="4dp"
                 android:topLeftRadius="4dp"
                 android:topRightRadius="4dp"/>
         </shape>
   </item>
   <item android:drawable="@drawable/yourImage" />
</layer-list>

2
"最初的回答" 可以像这样动态地完成所有操作。
 Button button = findViewById(R.id.button);
 button.setText("Test");
 button.setBackground(getRoundRect());

通过使用getRoundRect(),您可以获得所需颜色和大小的圆角形状。"Original Answer"的意思是"最初的回答"。请注意保留HTML标签。
public Drawable getRoundRect() {
    RoundRectShape rectShape = new RoundRectShape(new float[]{
            10, 10, 10, 10,
            10, 10, 10, 10
    }, null, null);
    ShapeDrawable shapeDrawable = new ShapeDrawable(rectShape);
    shapeDrawable.getPaint().setColor(Color.parseColor("#FF0000"));
    shapeDrawable.getPaint().setStyle(Paint.Style.FILL);
    shapeDrawable.getPaint().setAntiAlias(true);
    shapeDrawable.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);
    return shapeDrawable;
}

如果您想制作渐变圆角形状,您可以轻松地使用以下代码:
最初的回答
 GradientDrawable gd = new GradientDrawable(
            GradientDrawable.Orientation.TOP_BOTTOM,
            new int[] {0xFF616261,0xFF131313});
    gd.setCornerRadius(10f);
    button.setBackground(gd);

我的建议是通过动态文本、圆角和颜色制作按钮。

祝你好运,玩得开心(;


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接