How can I create my own theme for alertDialog?

后端 未结 5 842
有刺的猬
有刺的猬 2021-01-16 13:21

I want to create a different theme for all of alertDialog instances. I need my own title view instead of the usual black title background. All text should have a blue color,

相关标签:
5条回答
  • 2021-01-16 14:09

    please check this link. here am creating my own alertDialog class and its a simple method if you reuse the alertDialog in many situation in your application

    how Create setAdapter() for a AlertDialog

    0 讨论(0)
  • 2021-01-16 14:10

    You can use layout as Different themes, styles, Backgrounds

    customdialog.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/relativeLayout1"
        style="@android:style/Theme.Black.NoTitleBar.Fullscreen"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <LinearLayout
            android:id="@+id/layoutsample"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="vertical" >
    
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/dialoghdrbg"
                android:orientation="horizontal" >
    
                <ImageView
                    android:id="@+id/dialogheaderimage1"
                    android:layout_width="50dp"
                    android:layout_height="48dp" 
                    android:scaleType="fitXY"
                    />
    
                <TextView
                    android:id="@+id/dialogheadertext1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical|right"
                    android:layout_marginLeft="1dp"
                    android:gravity="center_vertical|right"
                    android:textColor="#000000"
                    android:textSize="25dp"
                    android:textStyle="bold" />
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="@drawable/dialogcontentbg"
                android:orientation="vertical" android:gravity="center">
    
                <TextView
                    android:id="@+id/dialogmessgetext1"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:textColor="#ffffff"
                    android:textSize="23dp" 
                    android:text="" 
                    android:gravity="center" 
                    />
    
    
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center|center_horizontal"
                    android:gravity="center|center_horizontal"
                    android:layout_marginTop="20dip" 
                    android:orientation="horizontal">
    
                  <Button
                        android:id="@+id/dialogokbutton1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="15dp"
                        android:background="@drawable/buttonanimation"
                        android:text="Retry"
                        android:textSize="20dp"
                        android:textStyle="bold" />
    
                    <Button
                        android:id="@+id/dialogcancelbutton1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="15dp"
                        android:background="@drawable/buttonanimation"
                        android:text="Report"
                        android:textSize="20dp"
                        android:textStyle="bold" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    
    </RelativeLayout>
    

    customDialog.java

    final Dialog favDialog = new Dialog(Myclass.this,
                android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
    
    
    favDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        favDialog.getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    
        favDialog.setContentView(R.layout.reloadurlpopuplayout);
    
        favDialog.setCancelable(false);
    
        ImageView dialogImage = (ImageView) favDialog
                .findViewById(R.id.dialogheaderimage1);
    
        dialogImage.setBackgroundResource(R.drawable.questionmark);
    
        TextView dialogMesage = (TextView ) favDialog
                .findViewById(R.id.dialogmessgetext1);
        TextView dialogHeader = (TextView) favDialog
                .findViewById(R.id.dialogheadertext1);
    
        String descText = getString(R.string.RetryReportMessage);
    
    
        dialogMesage.setBackgroundColor(0x00000000);
    
        dialogMesage.setText(descText);
    
        dialogHeader.setText(R.string.BrockenLinkHeader);
    
        try {
            favDialog.show();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    0 讨论(0)
  • 2021-01-16 14:22

    To use a specific style, use

    ctw = new ContextThemeWrapper(this, R.style.MyStyle);
    new AlertDialog.Builder(ctw)
        .setTitle(...)
    
    0 讨论(0)
  • 2021-01-16 14:26

    I solved my problem by creating a class which extends Dialog, and I created my own functions. for example setMessage,setTitle,setPositiveButton,setNegativeButton etc.

    But am confusing on how we can replace the setAdapter() and setSingleChoice().

    0 讨论(0)
  • 2021-01-16 14:27

    Here one example For Dialog:

    button = (Button) findViewById(R.id.button1);
    
                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("Give a Title...");
    
                    // set the custom dialog components - text, image and button
                    TextView text = (TextView) dialog.findViewById(R.id.text);
                    text.setText("Put here any custom text!");
                    ImageView image = (ImageView) dialog.findViewById(R.id.image);
                    image.setImageResource(R.drawable.ic_launcher);
    
                    Button dialogButton = (Button) dialog.findViewById(R.id.ButtonOK);
                    // if button is clickedthen dialog will closed
                    dialogButton.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });
    
                    dialog.show();
                  }
                });
    

    Custom AlertDialog

        AlertDialog.Builder builder;
        AlertDialog alertDialog;
        Context mContext;
        LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
        View urlayoutfile = inflater.inflate(R.layout.custom_dialog_xmlfile,(ViewGroup)findViewById(R.id.Yourlayout_id));
        builder = new AlertDialog.Builder(this);
        builder.setView(urlayoutfile);
        alertDialog = builder.create(); 
        alertDialog.show();
    
    0 讨论(0)
提交回复
热议问题