2013-01-23 70 views
7

私はカスタムレイアウトでAlertDialogを使用しています。レイアウトのTextViewの色は黒です。したがって、Android 4.0でHolo.Lightでダイアログを開くと、テキストが表示されます。しかし、Android 2.2でダイアログを開くと、灰色の背景のためにテキストが表示されません。背景色を変更する方法はありますか?AlertDialog背景色

+1

から:

はのは
//場所リストビューの項目のためのモデルを見てみましょう。 – hardartcore

+0

カスタムDlertDilaogを作成することができます。 [this](http://stackoverflow.com/a/5392617)を参照してください。 – burnO

答えて

1

ちょうどあなたが望む色にあなたのダイアログのlayout.xmlファイルのルートビューの背景を定義します。このよう

:あなたは、Android 2.2で、ダイアログを開いた場合

<?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

ダイアログの本体では正常に機能しますが、タイトルとフッターのOKボタン付きヘッダーはシステム定義の "暗い"色です(OS 2.3の場合)。 –

0

この機能を使用すると、レイアウトをあなたのダイアログに展開することができます。今あなたがレイアウト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

しかしテキストがあるため灰色の背景で表示されません。背景色を変更する方法はありますか?

はい、私はDialogBu​​ilderを使用して自分のアプリで使用できます。 はちょうどあなたのダイアログビルダーに真

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

にinverseBackgroundForcedを置きます。これは、Froyoの前にアンドロイド版でバックグラウンドを(暗い灰色の代わりに)白色に強制します。

+0

ビューの背景を設定するよりもクリーナーが追加されています(ビューは白く表示されますが、周囲はすべて灰色になります)。私はこの質問を見つける前にかなりの時間それを試していた。ありがとう。 :) – Sufian

+0

受け入れられた答えと同じですが、私はここでコメントすることができます:それはダイアログの本体には問題ありませんが、タイトルとフッターのOKボタン付きヘッダーはシステム定義の "暗い"色です(OS 2.3用)。 –

+0

それは私のために働いた、ありがとうたくさん – Naruto

1

はStinePikeのアイデアは非常に良好であるStinePikeとArtjom B.

にありがとうございました。

カスタマイズされた背景を持つAlertDialogにTextViewを配置しました。

ソリッドとグラデーションの背景を使用してオブジェクトをカスタマイズする方法を示します。

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

ありがとう、あなたの助けを大変ありがとう。トピックに私の答えを保持するためにカスタマイズされた背景を持つAlertDialogにTextViewを配置しました。私はあなたの優れたアドバイス "スタンドアローンアンサー"を休みました。コメントの横に画像を挿入する方法を教えてください。 –

1

"背景色を変更する方法はありますか?" はい異なるコンテキストにはいくつかの方法があります。

「詳細を提供し、私の研究をあなたに共有してください」と言わせてください。 私のコードは、Alert Dialogに組み込まれたListViewの項目のTextView Backgroundをカスタマイズする方法を示しています。ちょうどあなたのダイアログの表示を設定するよりも、XMLファイルにカスタムレイアウトを宣言し、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