我對AlertDialog使用自定義佈局。 TextView在佈局中的顏色是黑色的,所以在Android 4.0上用Holo.Light打開對話框時,文本是可見的。但是,如果您在Android 2.2中打開對話框,則由於灰色背景,文字不可見。有沒有辦法改變背景顏色?AlertDialog背景顏色
回答
只要定義爲您的對話框,你想要一個顏色layout.xml文件中的根視圖的背景。
像這樣:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/dialog_background" >
...
對話框的主體可以正常工作,但帶有標題和頁腳的頁眉仍然是系統定義的「黑暗」顏色(對於OS 2.3)。 –
AlertDialog.Builder.setView(getLayoutInflater().inflate(R.layout.your_layout, null));
通過使用此功能,您可以將佈局膨脹爲對話。現在在layout xml中做任何你想要的。例如看下面的代碼。
AlertDialog.Builder about = new AlertDialog.Builder(this);
about.setTitle(getString(name));
about.setIcon(R.drawable.icon);
about.setView(getLayoutInflater().inflate(R.layout.your_layout, null));
然而,如果你打開的Android 2.2文本對話框是因爲灰色背景的不可見。有沒有辦法改變背景顏色?
是的,這是可能的,我用我的應用程序使用DialogBuilder。 只要把inverseBackgroundForced真
builder.setInverseBackgroundForced(true);
AlertDialog dialog = builder.create();
dialog.show();
在你的對話框建設者。它會強制Froyo之前的Android版本的背景爲白色(而不是深灰色)。
非常感謝您對StinePike和Artjom B.
StinePike的想法是非常好的。
我把一個TextView放在具有自定義背景的AlertDialog中。
我演示瞭如何使用實體和漸變背景來自定義對象。
請讓我向您介紹我應用StinePike的想法的背景。
//地點:MainActivity.java
AlertDialog mAlertDialog_With_Radio_Buttons;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ini();
}
public void onAlert_With_Radio_Buttons_Close_Click(View view) {
mAlertDialog_With_Radio_Buttons.dismiss();
} // onAlert_With_Radio_Buttons_Close_Click
public void alert_with_radio_buttons(){
AlertDialog.Builder
mAlertDialog_Builder = new AlertDialog.Builder(this);
mAlertDialog_Builder
.setView(getLayoutInflater()
.inflate(R.layout.alert_with_radio_buttons, null));
mAlertDialog_Builder
.setTitle("Select The Directory");
mAlertDialog_With_Radio_Buttons = mAlertDialog_Builder.create();
mAlertDialog_With_Radio_Buttons.show();
} // public void alert_with_radio_buttons(){
// location: alert_with_radio_buttons.xml in layout
<LinearLayout
android:id="@+id/alert_with_radio_buttons_tv_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/turquoise1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<TextView
android:id="@+id/mAlert_With_Radio_Buttons_TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:background="@color/turquoise2"
android:textSize="@dimen/main_wiz_size"
android:text = "@string/alert_with_rb_tv_text" />
</LinearLayout>
// Location: colors in values
<color name="turquoise1">#FF00ABAB</color>
<color name="turquoise2">#FF00BCBC</color>
// Location: strings in values
<string name="alert_with_rb_tv_text">Directory Names</string>
// Close Definition
// location: alert_with_radio_buttons.xml in layout
<Button
android:id="@+id/alert_with_radio_buttons_close_btn"
android:text="@string/alert_with_radio_buttons_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btn_decor"
android:onClick="onAlert_With_Radio_Buttons_Close_Click" />
// location: btn_decor.xml in drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true">
<gradient
android:startColor="#700000ff"
android:endColor="#70009B80"
android:angle="-90"/>
</shape>
location: strings.xml in values
<string name="alert_with_radio_buttons_close">Close</string>
非常感謝您的幫助。我將TextView放入AlertDialog中,並具有自定義背景,以便在主題中保留我的答案。我暫停你的優秀建議「獨立答案」。請幫助我瞭解如何在我的評論旁插入圖片。 –
「有沒有辦法來改變背景顏色?」 是的,有不同的上下文有幾種方法。
請讓我「爲您提供詳細信息和分享我的研究」。 我的代碼顯示瞭如何爲警報對話框中包含的ListView項目獲取自定義的TextView背景。
讓我們從模型的ListView的項目開始
//地點:從佈局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/layer_border">
<TextView
android:id="@+id/text_view_for_lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity ="center"
android:padding ="5dip"
android:background="@color/turquoise2"
android:textSize="@dimen/lv_text_size"
android:textColor="@color/blue0"/>
</LinearLayout>
// location: main_activity.xml in layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/decor"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/main_activity_files_btn_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/layer_border" >
<Button
android:text="Files"
android:id="@+id/files_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btn_decor"
android:onClick="onMainActivity_Files_Click" />
</LinearLayout>
</LinearLayout>
// location: colors.xml in values
<color name="blue0">#0000FF</color>
<color name="turquoise2">#FF00BCBC</color>
// location: dimens.xml in values
<dimen name="lv_text_size">24dp</dimen>
// location: layer_border.xml in drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1dp" android:color="#9999FF" />
<solid android:color="#CCCCFF" />
<padding android:left ="4dp" android:top="4dp"
android:right="4dp" android:bottom="4dp" />
<corners android:radius="4dp" />
</shape>
// location: decor.xml in drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true">
<gradient
android:startColor="#aa0000ff"
android:endColor="#aa009B80"
android:angle="-90"/>
</shape>
// location: MainActivity.java
ListView mListView;
AlertDialog mAlertDialog;
ArrayAdapter<String> mArrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = new ListView(this);
ArrayList<String>
mArrayList_Days = new ArrayList<>();
for(int i = 0; i< 32; i++)
mArrayList_Days.add("Day " + String.valueOf(i));
mArrayAdapter = new ArrayAdapter<>(
this, R.layout.customized_tv_for_list_view,
R.id.text_view_for_lv, mArrayList_Days);
mListView.setAdapter(mArrayAdapter);
mListView
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String sel_item = (String) mListView
.getItemAtPosition(position);
Toast.makeText(MainActivity.this, sel_item, Toast.LENGTH_SHORT).show();
mAlertDialog.cancel();
} // onItemClick
}); // .setOnItemClickListener
build_files_alert_dialog();
}
public void build_files_alert_dialog() {
AlertDialog.Builder
mAlertBuilder = new AlertDialog.Builder(MainActivity.this);
mAlertBuilder.setTitle("Days");
mAlertBuilder.setView(mListView);
mAlertDialog = mAlertBuilder.create();
WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams();
mLayoutParams.copyFrom(mAlertDialog.getWindow().getAttributes());
}
public void onMainActivity_Files_Click(View view) {
mAlertDialog.show();
} // onMainActivity_Files_Click
- 1. 如何設置AlertDialog背景顏色
- 2. Android AlertDialog - 標題背景顏色
- 3. 背景或背景顏色?
- 4. 背景顏色
- 5. 背景顏色
- 6. 背景顏色
- 7. 背景顏色
- 8. 顏色背景
- 9. CSS3PIE背景褪色背景顏色
- 10. Uipicker背景顏色
- 11. NSImageView +背景顏色
- 12. DBVisualizer背景顏色
- 13. 背景顏色,ForegroundColor
- 14. QSciScintilla背景顏色
- 15. 1背景顏色
- 16. 背景顏色行
- 17. Win32背景顏色
- 18. FieldSet背景顏色
- 19. ItemControl背景顏色
- 20. DataGridViewTextBoxEditingControl背景顏色
- 21. iframe背景顏色
- 22. Bootstrap:Div背景顏色
- 23. CSS背景顏色
- 24. 與背景顏色
- 25. ListBoxItem背景顏色
- 26. Bootstrap背景顏色
- 27. DGML背景顏色
- 28. UIActivityViewController背景顏色
- 29. TodayExtension背景顏色
- 30. JQuery背景顏色
customized_tv_for_list_view.xml在XML文件中聲明你的自定義佈局,並不僅僅是爲您的對話視圖。 – hardartcore
您可以自定義DlertDilaog。請參閱[this](http://stackoverflow.com/a/5392617)。 – burnO