我有一排按鈕,我編程設置其選擇器的背景和文本。我想這樣做的原因是因爲,我有一套主題,用戶可以選擇,並根據所選的主題,我想改變按鈕的選擇器。組選擇編程問題
例如,如果用戶選擇了藍色的主題,在加載時,所述按鈕的背景是藍色和文本顏色爲白色。當他按下按鈕時,背景變爲白色,文字顏色變爲藍色。當用戶從按鈕中移除手指時,所做的更改會恢復爲默認背景爲藍色,而白色爲文本顏色。您可以在下面看到藍色的各個選擇器。
這與所有其他主題。我爲所有主題分開了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