2011-05-09 6 views
9

Bir uyarı kutusunun başlık çubuğunun arka plan rengini nasıl değiştirebilirim?Set AlertBox Başlık Çubuğu Arka Plan Rengi

AlertDialog.Builder alert=new AlertDialog.Builder(getParent()); 
alert.setTitle("sample"); 
alert.show(); 
+0

Bu yardımcı olabilir: [Android'de Özel Uyarı Kutusu Oluşturma.] (Http://stackoverflow.com/questions/3928562/creating-custom-alert-box-in-android) –

+0

Şimdiye kadar verdiğim en iyi cevap : http://blog.supenta.com/2014/07/02/how-to-style-alertdialogs-like-a-pro/ –

cevap

10

kolay yolu iletişim uzanır ve parametre olarak bir stil almak yapıcı uygulayan bir sınıf oluşturarak bir diyalog alt sınıfı sağlamaktır. Daha sonra kendi özel düzeninizi yapın.

Kod iletişim göstermek için:

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(); 
} 

alt sınıf için kod:

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 
    } 

} 

tarzı: 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> 

ve son düzen: c ustom_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

Merhaba CornflakesDK, alertDialog kutusuna içerik listesini görüntülemek istiyorum. Başlık backgroung rengi değişti, ancak içerik listesini dislpay değil. Contentt.t.Items (list, new DialogInterface.OnClickListener() { @Override public void onClick (DialogInterface iletişim kutusu, int pos) içeriği listesini görüntülemek için bu kodu kullanıyorum { TODO Otomatik oluşturulan yöntem saplama}}); – Jeeva

+0

Ardından düzeni değiştirin. Gerekirse kendi düğmelerinizi ve düğmelerinizi ekleyebilirsiniz.OnClickListeners. – CornflakesDK

+0

Teşekkürler, listView iyi çalışıyor. – Jeeva

0

Sadece bu

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

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

gibi özel başlık ayarlayabilirsiniz ve custom_title düzeninde Eğer cevap @CornflakesDK ve Dan bu

<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

gibi özel başlık oluşturabilirsiniz @ Buz ruhu, özel iletişim kutusunu yapmak ve bakımını kolaylaştırmak için mevcut AlertDialog.Builder uygulamasını kullanabileceğinizi düşündüm.

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 etkinlik kodu İçinde

<?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> 

, Sonra

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

, sen DialogBuilder iç stil var ve ayrıca fonksiyonlarını kullanabilir AlertDialog.Builder.