我有一排按钮,我编程设置其选择器的背景和文本。我想这样做的原因是因为,我有一套主题,用户可以选择,并根据所选的主题,我想改变按钮的选择器。组选择编程问题
例如,如果用户选择了蓝色的主题,在加载时,所述按钮的背景是蓝色和文本颜色为白色。当他按下按钮时,背景变为白色,文字颜色变为蓝色。当用户从按钮中移除手指时,所做的更改会恢复为默认背景为蓝色,而白色为文本颜色。您可以在下面看到蓝色的各个选择器。
这与所有其他主题。我为所有主题分开了XML。文字颜色更改的选择器正常工作。问题在于按钮的背景选择器。
selector_background_blue.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/white" android:state_pressed="true"/>
<item android:drawable="@color/blue_500"/>
</selector>
color_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/blue_500"/>
<item android:color="@android:color/white"/>
</selector>
我有取决于所选择的主题,它返回被拉伸(选择器)的类。
public Drawable getButtonBackgrounds(String theme) {
Drawable drawable = null;
if (theme.equalsIgnoreCase(Const.Theme.BLUE))
drawable = context.getResources().getDrawable(
R.drawable.selector_background_blue);
return drawable;
}
我对按钮的背景如下设置这些选择:我如下得到选择
private void setButtonBackgrounds(Drawable buttonDrawable) {
int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
btnA.setBackgroundDrawable(buttonDrawable);
btnT.setBackgroundDrawable(buttonDrawable);
.....
.....
btnVoice.setBackgroundDrawable(buttonDrawable);
} else {
btnA.setBackground(buttonDrawable);
btnT.setBackground(buttonDrawable);
.....
.....
btnVoice.setBackground(buttonDrawable);
}
}
按钮的xml
:
<Button
android:id="@+id/btnT"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:background="?android:attr/selectableItemBackground"
android:text="@string/button_t"
android:textSize="22sp" />
总计行的XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<Button
android:id="@+id/btnA"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:background="?android:attr/selectableItemBackground"
android:text="@string/arithmetic_symbol"
android:textSize="16sp" />
<Button
android:id="@+id/btnT"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:background="?android:attr/selectableItemBackground"
android:text="@string/trigonometric_symbol"
android:textSize="16sp" />
<Button
android:id="@+id/btnN"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:background="?android:attr/selectableItemBackground"
android:text="@string/voice_calculator_symbol"
android:textSize="16sp"
android:visibility="gone" />
<ImageButton
android:id="@+id/btnVC"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:background="?android:attr/selectableItemBackground"
android:contentDescription="@string/empty"
android:src="@drawable/ic_keyboard_voice_black"
android:text="" />
<Button
android:id="@+id/btnC"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:background="?android:attr/selectableItemBackground"
android:text="@string/button_c"
android:textSize="16sp" />
<Button
android:id="@+id/btnD"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:background="?android:attr/selectableItemBackground"
android:text="@string/button_del"
android:textSize="16sp" />
</LinearLayout>
这对于该行中的所有按钮都是相同的。
提拉设置就好上的负载。请参考下面的图片。
的问题是当我点击一个按钮(用于离。,A)中,相邻的ImageButton(麦克风)也改变其状态。请看看下面的图片:
这究竟是为什么?有人可以帮我弄这个吗。如果您需要其他信息,请告诉我。
这些按钮的容器是什么类?我的意思是,它们是否包含在LinearLayout,FrameLayout或其他内容中?你有任何听众连接到这些按钮? – aga 2015-02-10 07:17:04
它们在线性布局内。将在1小时内用XML更新问题 – 2015-02-10 07:19:23
@aga,更新行的xml问题。每个按钮都有一个onClickListener附加到它。 – 2015-02-10 09:23:33