2013-01-23 89 views
7

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

+1

customized_tv_for_list_view.xml在XML文件中声明你的自定义布局,并不仅仅是为您的对话视图。 – hardartcore

+0

您可以自定义DlertDilaog。请参阅[this](http://stackoverflow.com/a/5392617)。 – burnO

回答

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" > 
... 
+1

对话框的主体可以正常工作,但带有标题和页脚的页眉仍然是系统定义的“黑暗”颜色(对于OS 2.3)。 –

0
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)); 
9

然而,如果你打开的Android 2.2文本对话框是因为灰色背景的不可见。有没有办法改变背景颜色?

是的,这是可能的,我用我的应用程序使用DialogBu​​ilder。 只要把inverseBackgroundForced真

builder.setInverseBackgroundForced(true); 
AlertDialog dialog = builder.create(); 
dialog.show(); 

在你的对话框建设者。它会强制Froyo之前的Android版本的背景为白色(而不是深灰色)。

+0

比设置添加视图的背景更清晰(它会显示视图是白色的,但在它周围会是灰色的)。在发现这个问题之前,我试了很长时间。谢谢。 :) – Sufian

+0

与接受的答案相同,我可以在这里发表评论:对话框的主体可以正常工作,但标题和带有OK按钮的页眉仍然是系统定义的“黑暗”颜色(对于OS 2.3)。 –

+0

它为我工作,非常感谢 – Naruto

1

非常感谢您对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> 
+0

非常感谢您的帮助。我将TextView放入AlertDialog中,并具有自定义背景,以便在主题中保留我的答案。我暂停你的优秀建议“独立答案”。请帮助我了解如何在我的评论旁插入图片。 –

1

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

请让我“为您提供详细信息和分享我的研究”。 我的代码显示了如何为警报对话框中包含的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