我創建了一個按鈕,我曾經android:background="@drawable/background"更改影像,但它只顯示按鈕的默認設計(紫色)。如何將其設定為我保存在可繪制檔案夾中的影像?
我知道有影像按鈕,但我想在按鈕上寫一個文本,但是如果我使用影像按鈕,我沒有可以在頂部寫文本的功能
uj5u.com熱心網友回復:
正如您所提到的,如果您使用過ImageButton.
據我了解,您希望查看在影像上方有文本,在文本底部有影像的視圖。
如果您使用android:background功能 on Button,按鈕的背景將是您的可繪制影像,它不會像您預期的那樣。
如果您確定要使用Button,您可以使用android:drawableBottom功能在文本下方顯示您的可繪制影像。您基本上可以使用android:text功能添加按鈕文本。
我對你的建議,你應該使用TextView而不是Button那種情況。因為如果你Button按照我上面提到的那樣做,你的按鈕會有背景,你可以使用下面的代碼為你的按鈕制作透明背景。
<Button
android:id="@ id/buttonMoreTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:drawableBottom="@drawable/ic_baseline_home_24"
android:text="@string/app_name"
android:textColor="@color/black" />
此代碼將使您Button的背景透明。
android:background="@android:color/transparent"
但這是一個有點hacky的解決方案。如果它沒有背景,并且如果您確定不必Button在您的情況下使用,您可以使用TextView它并且基本上在底部顯示文本和可繪制,您可以TextView基本上如下編碼。
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
app:drawableBottomCompat="@drawable/ic_baseline_home_24" />
注意:這些是非常簡單的 xml 示例,用于解決您在同一視圖中在文本底部顯示影像的主要問題。如果您需要更多資訊,請告訴我,我很樂意提供幫助。
uj5u.com熱心網友回復:
您必須使用 ImageView 并創建一個 clicker 方法來允許您單擊影像,您不必在想要單擊影像時使用按鈕
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/457388.html
上一篇:使用按鈕從另一個類更改某物的狀態
