Customizing the Alert dialog in Android

2019-04-09 14:52发布

问题:

I want to show text in dark color on a light background on alert dialog. But I can't figure out how to do this. Please help me.

Thanks.

回答1:

see this example,It will help you:http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application


as in this example layout defined in file for alert dialog.You can set your style for alert dialog.



回答2:

You can create your own layout in an XML View just like you would for an Activity:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/layout_root"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="10dp"
              >
    <ImageView android:id="@+id/image"
               android:layout_width="wrap_content"
               android:layout_height="fill_parent"
               android:layout_marginRight="10dp"
               />
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:textColor="#FFF"
              />
</LinearLayout>

Then you can use this View in the Dialog by calling setContentView(View) on the Dialog:

Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);

dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");

TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.android);

As the example shows, you'll have to set some of the values after you declare the content view.

Example provided from http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog