[英]Background image for a button in android
我正在嘗試為按鈕添加背景圖像,並且該圖像正在工作,但是沒有達到我的預期。
我希望背景僅覆蓋按鈕。 換句話說,圖像應具有與上方2個按鈕相同的形式(我說的是圓角和較小的尺寸)。
這是布局的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent">
<Button
android:onClick="playerStats"
android:text="@string/plStats"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
<LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent">
<Button
android:text="@string/comp"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
<LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent">
<Button
android:background="@drawable/calendar"
android:text="@string/calendar"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
這很容易做到
您應該使用ImageButton並刪除這些按鈕,例如
<ImageButton
android:src="@drawable/calendar"
android:text="@string/calendar"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
如果您想刪除該按鈕的背景,也可以使用:
android:background="@null"
用這些布局替換您的布局...可以為您提供幫助
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout android:layout_weight="1" android:layout_height="match_parent" android:layout_width="match_parent">
<Button
android:onClick="playerStats"
android:text="@string/plStats"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
<View ////I HAVE ADDED VIEW WHICH IS TRASNPARENT TO SEPRATE THE BUTTONS
android:layout_width="fill_parent"
android:layout_height="10dp"
android:background="@android:color/transparent" >
</View>
<LinearLayout android:layout_weight="1" android:layout_height="match_parent" android:layout_width="match_parent">
<Button
android:text="@string/comp"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
<View ////I HAVE ADDED VIEW WHICH IS TRASNPARENT TO SEPRATE THE BUTTONS
android:layout_width="fill_parent"
android:layout_height="10dp"
android:background="@android:color/transparent" >
</View>
<LinearLayout android:layout_weight="1" android:layout_height="match_parent" android:layout_width="match_parent">
<Button
android:background="@drawable/calendar"
android:text="@string/calendar"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
<View ////I HAVE ADDED VIEW WHICH IS TRASNPARENT TO SEPRATE THE BUTTONS
android:layout_width="fill_parent"
android:layout_height="10dp"
android:background="@android:color/transparent" >
</View>
最簡單的解決方案是:您需要編輯圖像背景(使其更小並帶有角)-使用Photoshop可以輕松完成
比使用9補丁工具制作9補丁圖像(9補丁圖像可以自動調整大小以適應視圖內容和屏幕尺寸)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.