我正在尝试创建一个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 button in this app类似。
答案 0 :(得分:1)
您可以使用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>
答案 1 :(得分:0)
您可以使用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 :(得分:0)
您可以像这样动态地进行所有操作
Button button = findViewById(R.id.button);
button.setText("Test");
button.setBackground(getRoundRect());
并通过getRoundRect()可以使用您所需要的颜色和大小获得圆角形状 想要
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);
我所有的建议都是通过动态文本以及圆角和颜色制作按钮
祝你好运,玩得开心(;