2013-03-29 128 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