[英]How to change background color of button with image in Android
在我的Android應用中,右側有帶有箭頭圖像的按鈕。 在我的SettingsActivity上,您可以更改應用程序的顏色(更改按鈕和TextViews的顏色)。
我很好地更改了TextViews的顏色,但是當我更改按鈕的顏色時,圖像(右側的箭頭)消失了。
這就是我的代碼。 它會更改顏色並刪除圖像。
loginButton = (Button) findViewById(R.id.btnLogin);
loginButton.setTextColor(color);
我在某個帖子上找到了這個。 但這僅適用於ImgageView,不適用於按鈕。
loginButton = (Button) findViewById(R.id.btnLogin);
GradientDrawable bgShape = (GradientDrawable)loginButton.getBackground();
bgShape.setColor(getResources().getColor(Color.BLACK));
text.setTextColor(color);
我也試圖改變這種顏色。 但這根本不會改變顏色。
Drawable button = context.getResources().getDrawable(R.drawable.button);
button.setColorFilter(new
PorterDuffColorFilter(0xffff00,Mode.MULTIPLY));
我希望應該做的最后一件事是為每種顏色定義新的可繪制對象。 但這真的很糟糕...我也認為,問題不僅在於圖像。 我認為此解決方案會覆蓋所有可繪制文件...
是否有人知道如何更改可繪制的顏色並將圖像保留在按鈕上?
編輯:只是嘗試...我可以將所有顏色移到colors.xml文件。 並將顏色路徑更改為所有可繪制文件。 喜歡
<resource>
Color 1
Color 2
Color 3
...
</resource>
但是,可繪制文件如何決定應使用哪種顏色呢?
EDIT2:按鈕布局:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
style="@style/bgGrey">
<RelativeLayout
android:layout_height="0dip"
android:layout_weight="30"
android:layout_width="fill_parent"
>
<TextView
...
/>
</RelativeLayout>
<LinearLayout
...>
<TextView
.../>
<EditText
.../>
<TextView
.../>
<EditText
.../>
<Button
android:id="@+id/btnLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/password"
android:gravity="center|left"
android:paddingLeft="15dp"
android:layout_marginTop="10dp"
android:background="@drawable/login_button_background"
android:text="Login" />
</LinearLayout>
</LinearLayout>
按鈕可繪制,background.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/layer"></item>
</selector>
在layer.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:left="0dp" android:top="5dp" android:bottom="5dp" android:right="0dp" >
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#ffb500"/>
</shape>
</item>
<item android:left="350dp" android:top="5dp" android:bottom="5dp" android:right="5dp" >
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#ffb500"/>
</shape>
</item>
<item android:right="15dp" >
<bitmap android:src="@drawable/btn_right" android:gravity="right" android:tileMode="disabled" />
</item>
</layer-list>
一種替代方法是:從背景中刪除箭頭,並將其設置為Compound Drawable 。
隨着Button
擴展TextView
,您可以將可繪制對象設置為頂部,底部,左側或右側。
在XML中,只需將屬性drawableRight
設置為箭頭,或通過JAVA代碼即可:
button.setCompoundDrawablesWithIntrinsicBounds (idLeft, idTop, idRight, idBottom)
因此,設置右箭頭:
button.setCompoundDrawablesWithIntrinsicBounds (0, 0, R.drawable.your_arrow, 0);
這樣,背景就獨立於箭頭,並且無需創建復雜的版本或選擇器即可更輕松地對其進行修改。
有一些方法和屬性可以調整可繪制的填充,以便您可以將它或多或少地放置在需要的位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.