[英]How to make a button with rounded corners, background image and text in android
我正在嘗試創建一個可以更改文本的 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 按鈕的結果,例如,如果這是一個按鈕,我也不知道。
你可以使用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>
你可以像這樣動態地完成所有這些
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);
我的所有建議都是通過動態文本和圓角和顏色制作按鈕
祝好運並玩得開心點 (;
您可以使用 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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.