Android上的AlertDialog.Builder样式

时间:2013-08-29 07:21:36

标签: android alertdialog

我想修改AlertDialog.Builder的标题和正文风格 儿子我使用setCustomTitle和setView来达到这个目的 以下是简化代码:

private AlertDialog dialog;
public void Showdialog() {
    LayoutInflater inflaterTitle = getLayoutInflater();
    View TitleView = inflaterTitle.inflate(layout, (ViewGroup)findViewById(id));
    TextView Title = (TextView)TitleView.findViewById(id);
    Title.setText("Title");

    LayoutInflater inflaterBody = getLayoutInflater();
    View BodyView = inflaterBody.inflate(layout, (ViewGroup)findViewById(id));
    TextView Body = (TextView)BodyView.findViewById(id);
    Body.setText("Body");

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCustomTitle(TitleView);
    builder.setView(BodyView);

    dialog = builder.create();
    dialog.show();
}

TitleView和BodyView with bule background。
显示对话框如下。
enter image description here
有2个黑色 我该如何删除它?

2 个答案:

答案 0 :(得分:8)

而不是那样,如果你想为对话框设置自定义布局,那么请使用下面的代码。

final Dialog dialog = new Dialog(SplashActivity.this,R.style.CustomDialogTheme);
        dialog.getWindow();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.internet_dialog);
        dialog.setCancelable(false);

        Button retryBtn = (Button) dialog.findViewById(R.id.retryBtn);
        Button cancel = (Button) dialog.findViewById(R.id.cancelBtn);

        retryBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
                checkForInternetAndNextFlow();
            }
        });

        cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
                finish();
            }
        });
        dialog.show();
值中的

styles.xml包含:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:fitsSystemWindows">true</item>

    <!-- <item name="android:colorBackgroundCacheHint">#00000000</item>
    <item name="android:background">#00000000</item> -->

</style>

这对你有很大的帮助。您可以根据需要设置任何视图。

答案 1 :(得分:3)

尝试使用自定义布局的Dialog,如下所示......

<强> RES /布局/ main.xml中:

<?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:orientation="vertical" >

    <Button
        android:id="@+id/buttonShowCustomDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Custom Dialog" />

</LinearLayout>

<强> RES /布局/ custom.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFF" 
        android:layout_toRightOf="@+id/image"/>/>

     <Button
        android:id="@+id/dialogButtonOK"
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:text=" Ok "
        android:layout_marginTop="5dp"
        android:layout_marginRight="5dp"
        android:layout_below="@+id/image"
        />

</RelativeLayout>

<强> MainActivity.java:

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

    final Context context = this;
    private Button button;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button = (Button) findViewById(R.id.buttonShowCustomDialog);

        // add button listener
        button.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View arg0) {

            // custom dialog
            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.custom);
            dialog.setTitle("Title...");

            // set the custom dialog components - text, image and button
            TextView text = (TextView) dialog.findViewById(R.id.text);
            text.setText("Android custom dialog example!");
            ImageView image = (ImageView) dialog.findViewById(R.id.image);
            image.setImageResource(R.drawable.ic_launcher);

            Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
            // if button is clicked, close the custom dialog
            dialogButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

            dialog.show();
          }
        });
    }
}