2015-12-12 30 views
5

Có cách nào để thay đổi màu nền tiêu đề của AlertDialog (android.support.v7.app.AlertDialog) ?? Hiện nay trong chủ đề của tôi, tôi cóMàu nền tiêu đề của Android AlertDialog

<item name="alertDialogTheme">@style/AppCompatAlertDialogStyle</item> 

<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert"> 
     <item name="colorAccent">@color/colorAccent</item> 
    </style> 

và tôi nhận được nó như thế này,

enter image description here

Làm thế nào tôi có thể làm cho nó trông như thế này,

enter image description here

Sử dụng

<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert"> 
     <item name="colorAccent">@color/colorAccent</item> 
     <item name="android:windowTitleStyle">@style/DialogTitle</item> 
    </style> 

    <style name="DialogTitle"> 
     <item name="android:background">@color/colorPrimary</item> 
    </style> 

cho

enter image description here

Bất kỳ ý tưởng về cách thức này có thể được thực hiện?

+0

bạn có tìm thấy giải pháp nào cho điều này không? – Hunt

+0

Tôi nghĩ câu trả lời của Mr.Songoku là câu trả lời hay nhất http://stackoverflow.com/a/42135263/7797592 – m7mdbook

Trả lời

0

Sử dụng tùy chỉnh Alerbox, sử dụng mã này trên click.I làm một Layout tùy chỉnh "alert_input" và OK và Cancel tùy chọn sẽ được hiển thị

LayoutInflater layoutInflater = LayoutInflater.from(Login.this); 
       View promptView = layoutInflater.inflate(R.layout.alert_input, null); 
       final EditText editText = (EditText) promptView.findViewById(R.id.alertEdit2); 
       final EditText editText2 = (EditText) promptView.findViewById(R.id.alertEdit3); 
       final TextView at=(TextView)findViewById(R.id.alertText); 

       AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Login.this,AlertDialog.THEME_HOLO_LIGHT); 
       alertDialogBuilder.setView(promptView); 
       alertDialogBuilder.setCancelable(false) 
         .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 


          } 
         }) 
         .setNegativeButton("Cancel", 
           new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, int id) { 
             dialog.cancel(); 
            } 
           }); 

       // create an alert dialog 
       AlertDialog alert = alertDialogBuilder.create(); 
       alert.show(); 
6

Bạn chỉ có thể thiết lập tiêu đề tùy chỉnh như thế này

LayoutInflater inflater = this.getLayoutInflater(); 

    View titleView = inflater.inflate(R.layout.custom_title, null); 

    new AlertDialog.Builder(SubCategoryActivity.this) 
         .setCustomTitle(titleView); 

và trong bố cục custom_title, bạn có thể tạo tiêu đề tùy chỉnh như thế này

<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

Cảm ơn câu trả lời của bạn:) - đẹp – Kalanidhi

Các vấn đề liên quan