在我的Android应用程序中,我的右侧有箭头图像按钮。在我的SettingsActivity上,您可以更改app的颜色(更改按钮和TextView的颜色)。
我改变了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));
我希望应该做的最后一件事是为每种颜色定义新的drawable。但这太糟糕了...... 我也认为,问题不仅在于图像问题。我认为这个解决方案会覆盖所有可绘制文件......
是否有人知道如何更改drawable的颜色并将图像保留在按钮上?
编辑:只是尝试...我可以将所有颜色移动到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>
Button drawable,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>
答案 0 :(得分:1)
另一种方法可以是:从背景中移除箭头并将其设置为 Compound Drawable 。
当Button
延伸TextView
时,您可以将drawable设置为顶部,底部,左侧或右侧。
在XML中,只需将属性drawableRight
设置为箭头,或通过JAVA代码:
button.setCompoundDrawablesWithIntrinsicBounds (idLeft, idTop, idRight, idBottom)
所以设置右箭头:
button.setCompoundDrawablesWithIntrinsicBounds (0, 0, R.drawable.your_arrow, 0);
这样,背景与箭头无关,并且在不创建复杂版本或选择器的情况下更容易修改背景。
有一些方法和属性可以调整可绘制的填充,因此您可以将它或多或少地放置在您需要的地方。