AlertDialog背景颜色

时间:2013-01-23 14:30:15

标签: android android-alertdialog

我正在使用具有自定义布局的AlertDialog。布局中TextView的颜色为黑色,因此在使用Holo.Light在Android 4.0上打开对话框时,文本是可见的。但是,如果您在Android 2.2中打开对话框,则由于灰色背景,文本不可见。有没有办法改变背景颜色?

5 个答案:

答案 0 :(得分:9)

  

但是,如果您在Android 2.2中打开对话框,则由于灰色背景,文本不可见。有没有办法改变背景颜色?

是的,我可以使用DialogBu​​ilder在我的应用程序上使用它。 只需将inverseBackgroundForced置为true

即可
builder.setInverseBackgroundForced(true);
AlertDialog dialog = builder.create();
dialog.show();
对话框构建器上的

。在Froyo之前,它会在android版本上强制背景为白色(而不是深灰色)。

答案 1 :(得分:1)

只需在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" >
...

答案 2 :(得分:1)

非常感谢StinePike和Artjom B.

StinePike的想法非常好。

我在AlertDialog中放置了一个具有自定义背景的TextView。

我展示了如何使用实体和渐变背景来自定义对象。

请让我向您介绍我应用StinePike的想法的背景。

// location: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>

答案 3 :(得分:1)

“有没有办法改变背景颜色?” 是的,有几种方法可以用于不同的上下文。

请让我“向您提供详细信息并分享我的研究”。 我的代码显示了如何为Alert Dialog中包含的ListView项获取自定义TextView Background。

让我们从ListView项目的模型开始     // location:来自布局的customized_tv_for_list_view.xml     

<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

答案 4 :(得分:0)

AlertDialog.Builder.setView(getLayoutInflater().inflate(R.layout.your_layout, null));

通过使用此功能,您可以将布局扩展到对话框。现在在布局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));