Set AlertBox Title Bar Background Color

后端 未结 3 735
囚心锁ツ
囚心锁ツ 2021-02-14 21:11

How can I change the background color for an alertbox\'s title bar?

AlertDialog.Builder alert=new AlertDialog.Builder(getParent());
alert.setTitle(\"sample\");
a         


        
相关标签:
3条回答
  • 2021-02-14 21:51

    From the answer @CornflakesDK and @ice spirit, I thought you can use the current AlertDialog.Builder implementation to do the custom dialog and make it easy to maintain.

    CustomDialogBuilder.java

    public class CustomDialogBuilder extends AlertDialog.Builder {
    
      private View view;
    
      public CustomDialogBuilder(Context context) {
        super(context);
        view = LayoutInflater.from(getContext()).inflate(R.layout.custom_dialog_title, null);
        setCustomTitle(view);
      }
    
      @Override
      public Builder setTitle(int titleId) {
        TextView titleTextView = view.findViewById(R.id.exemptionSubHeading4);
        titleTextView.setText(getContext().getString(titleId));
        return this;
      }
    
      @Override
      public Builder setTitle(CharSequence title) {
        TextView titleTextView = view.findViewById(R.id.exemptionSubHeading4);
        titleTextView.setText(title);
        return this;
      }
    }
    

    custom_dialog.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical">
      <LinearLayout
        android:id="@+id/llsubhead"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:background="@color/black"
        android:orientation="vertical">
        <TextView
          android:id="@+id/exemptionSubHeading4"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:layout_margin="15dp"
          android:layout_gravity="center"
          android:text="Exemption Sub Head"
          android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
          android:textColor="@color/white" />
      </LinearLayout>
    </LinearLayout>
    

    Inside your activity code,

     new CustomDialogBuilder(MyActivity.this)
                      .setTitle(R.string.actions)
                      .setItems(R.array.items_actions, (dialog, which) -> {
                        // handle items
                      }).create().show();
    

    Then, you can have styling inside the DialogBuilder and also utilize the functions of the AlertDialog.Builder.

    0 讨论(0)
  • 2021-02-14 21:54

    The easiest way is to subclass a dialog by creating a class which extends dialog and implements the constructor which take style as a parameter. Then make your own custom layout to it.

    The Code to show the dialog:

    private void showDialog()
    {
        Custom_Dialog dialog = new Custom_Dialog(this, R.style.myCoolDialog);
    
        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.icon);
    
        dialog.show();  
    }
    

    The code for the subclass:

    package com.stackoverflow;
    
    import android.app.Dialog;
    import android.content.Context;
    
    public class Custom_Dialog extends Dialog {
    
        protected Custom_Dialog(Context context, int theme) {
            super(context, theme);
            // TODO Auto-generated constructor stub
        }
    
    }
    

    the style: myCoolDialog.xml

    <resources>
        <style name="myCoolDialog" parent="android:Theme.Dialog"> 
            <item name="android:windowBackground">@drawable/blue</item>
            <item name="android:colorForeground">#f0f0</item> 
         </style> 
    </resources>
    

    and last the layout:custom_dialog.xml

    <?xml version="1.0" encoding="utf-8"?>
    <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>
    
    0 讨论(0)
  • 2021-02-14 21:55

    You can just set custom title like this

    LayoutInflater inflater = this.getLayoutInflater();
    View titleView = inflater.inflate(R.layout.custom_title, null);
    
    new AlertDialog.Builder(SubCategoryActivity.this)
                        .setCustomTitle(titleView);
    

    and in custom_title layout you can create custom title like this

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:id="@+id/llsubhead"
            android:background="@color/colorPrimary">
    
            <TextView
                android:id="@+id/exemptionSubHeading4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:layout_weight="1"
                android:text="Exemption Sub Head"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
                android:textColor="@color/white" />
        </LinearLayout>
    </LinearLayout>
    
    0 讨论(0)
提交回复
热议问题