2013-03-29 51 views
1

我試圖設計一個AlertDialog來取悅客戶。他們喜歡 Theme.Holo.Light.Dialog上的藍色標題欄,但喜歡另一主題的綠色複選框。 這是我想生產什麼:對話框複選框上的自定義樣式

enter image description here

所以,我已經有了一個樣式定義是這樣的:

<style name="MyDialogTheme" parent="@android:Theme.Holo.Light.Dialog"> 
      <item name="android:listChoiceIndicatorMultiple">@drawable/checkbox_green</item> 
</style> 

<style name="mycheckbox" parent="@android:style/Widget.CompoundButton.CheckBox"> 
    <item name="android:button">@drawable/checkbox_box</item> 

</style> 

,我已經得到了checkbox_green的定義如下這是隻是PNG文件:

<item android:state_checked="false" 
    android:drawable="@drawable/checkbox_unchecked" /> 

<item android:state_checked="true" 
    android:drawable="@drawable/checkbox_checked"/> 

</selector> 

和創建我的對話建設者用特異性IC主題在Java中,像這樣:

ContextThemeWrapper ctw = new ContextThemeWrapper(mContext, R.style.MyDialogTheme); 
AlertDialog.Builder builder= new AlertDialog.Builder(ctw); 

但我不能得到這個主題,以顯示綠色複選框,而不是藍色的對話框。 我得到這個: enter image description here

我會繼續前進,創造一個完整的佈局,然後使用這樣的:

AlertDialog shareDialog = new AlertDialog.Builder(mContext).create(); 
LayoutInflater inflater = MainActivity.this.getLayoutInflater(); 

View dialogView = null; 
dialogView = inflater.inflate(R.layout.share, (ViewGroup) getCurrentFocus()); 
shareDialog.setView(dialogView); 

但是這需要所有樣式對話框,而不僅僅是checkboxes.It似乎如此簡單的重新設計複選框的風格,但我無法做到這一點。

除了創建一個完整的佈局和用於獲取綠色複選框而不是藍色,我必須做什麼?

回答

0

應該如下去...

第一個設計,只要你想在任何兼容的格式(JPEG,PNG)...,並放置在繪製文件夾的複選框...

然後進行按鈕和選擇圖像單獨的XML你專爲選中和未選中複選框...,並放置在適當的名稱項目的繪製文件夾這個文件customchk.xml說這裏...

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_checked="false" android:drawable="@drawable/chk2" /> //custom image 
<item android:state_checked="true" android:drawable="@drawable/chk1" /> //custom image 
<item android:drawable="@drawable/chk2" /> <!-- default --> 
</selector> 

然後讓你的複選框:按鈕設置爲您的XML文件...如下

<CheckBox 
      android:id="@+id/notifs" 
      android:layout_width="150dp" 
      android:button="@drawable/customchk"  //your xml 
      android:layout_height="wrap_content" 
/> 

我想它應該工作,你不必改變所有的對話......

+0

是的,我已經這樣做了,但需要創建一個整體佈局來容納複選框,而不是簡單地重新設計那些對話框將要構建的樣式 – Martin

+0

我想你想要改變內置樣式複選框rahter比創造新的... mi好 –

+0

我想這會幫助...有些人提供了我這個...希望它也適用於你... http://android-holo-colors.com/ –

-1

據我所知,沒有辦法到對話框而不創建自定義的只是風格配件查看對象。

創建類似的風格:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="AlertDialogCustom" parent="@android:style/AlertDialog"> 
     <item name="android:textColor">#00FF00</item> 
     <item name="android:typeface">monospace</item> 
     <item name="android:textSize">10sp</item> 
    </style> 
</resources> 

充氣觀點一樣:

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogCustom)); 

Here are attributes for checkboxes與您的自定義AlertDialog風格使用。

+0

嘗試設計checkMark和其他人的樣式,但仍然獲得主題中定義的藍色。 – Martin

2

實際上,您可以更改多選對話框中的複選框。

您需要爲您的對話框定製適配器,才能訪問列表視圖。然後,您可以調用類CheckedTextView的方法setCheckMarkDrawable

下面是一個例子:

enter image description here

文件default_checkbox.xmlres/drawable

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

    <item android:state_checked="true" 
     android:drawable="@drawable/checkbox_checked" /> <!-- checked --> 

    <item android:state_pressed="true" 
     android:drawable="@drawable/checkbox_checked" /> <!-- pressed --> 

    <item android:drawable="@drawable/checkbox_default" /> <!-- default --> 

</selector> 

文件DialogUtil.java

package example.dialog; 

import android.app.AlertDialog; 
import android.content.Context; 
import android.util.Log; 
import android.view.*; 
import android.widget.*; 
import android.widget.AdapterView.OnItemClickListener; 

public class DialogUtil { 

    private DialogUtil() { 
    } 

    public static AlertDialog show(Context context) { 
     String[] items = {"text 1", "text 2", "text 3"}; 
     AlertDialog.Builder builder = new AlertDialog.Builder(context); 
     builder.setTitle("Test") 
      .setPositiveButton("OK", null) 
      .setAdapter(new CustomAdapter(context, items), null); 
     AlertDialog dialog = builder.show(); 

     ListView list = dialog.getListView(); 
     list.setItemsCanFocus(false); 
     list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
     list.setOnItemClickListener(listener); 
     return dialog; 
    } 

    private static OnItemClickListener listener = new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      Log.i("DialogUtil", "Clicked on " + view); 
     } 
    }; 

    private static class CustomAdapter extends ArrayAdapter<String> { 

     public CustomAdapter(Context context, String[] array) { 
      super(context, android.R.layout.simple_list_item_multiple_choice, array); 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View view = super.getView(position, convertView, parent); 
      if (view instanceof CheckedTextView) { 
       CheckedTextView checkedView = (CheckedTextView) view; 
       checkedView.setCheckMarkDrawable(R.drawable.default_checkbox); 
      } 
      return view; 
     } 
    } 
} 

注意:如果您只是使用AlertDialog,那麼在獲取ListView之前,請先撥打show,首先,如上所述。

但是,如果您使用DialogFragmentonCreateDialog,那麼您將獲得ListView,內部爲onStart