在对话框复选框自定义样式 [英] custom style on dialog checkbox

查看:514
本文介绍了在对话框复选框自定义样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想格调AlertDialog取悦客户。双方就喜欢蓝色标题栏
Theme.Holo.Light.Dialog但像其他主题的绿色复选框。
这就是我要生产什么:

I'm trying to style an AlertDialog to please a customer. They like the Blue title bar on Theme.Holo.Light.Dialog but like the green checkboxes from another theme. this is what I want to produce:

所以,我已经有了一个样式定义是这样的:

So, I've got a style definitions like this:

<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文件:
    
    

and I've got a definition for checkbox_green as follows which are just PNG files:

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

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

</selector>

和我创建对话框建设者在Java中,像这样一个特定的主题:

and I create my dialog builder with a specific theme in Java like so:

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

但我不能得到这个主题,显示绿色复选框,而不是蓝色的对话框。
我得到这个:

But I cannot get the dialog to display green checkboxes instead of the blue in this theme. I get this:

我可以继续前进,创建一个完整的布局,然后使用这样的:

I could go ahead and create an entire layout and then use that like this:

 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似乎非常简单的,只是重新风格的复选框,但我无法使这项工作。

but that requires styling all of the dialog, not just the checkboxes.It seems so much simpler to just re-style the checkboxes but I'm not able to make that work.

我必须做什么,除了建立一个完整的布局,并用它来获得绿色复选框,而不是蓝色的?

what must I do, other than to create a complete layout and use to get the green checkboxes rather than the blue?

推荐答案

其实,你可以改变复选框中多选择对话框中。

Actually, you CAN change the checkbox inside a multi-choice dialog.

您需要自定义适配器为您的对话,这样才能有机会获得名单的意见。
然后,您调用方法<一href=\"http://developer.android.com/reference/android/widget/CheckedTextView.html#setCheckMarkDrawable%28android.graphics.drawable.Drawable%29\"相对=nofollow> setCheckMarkDrawable 类CheckedTextView的。

You need a custom adapter for your dialog, so as to have access to the views of the list. Then, you call method setCheckMarkDrawable of class CheckedTextView.

下面是一个例子:

文件 default_checkbox.xml 水库内/绘制

<?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之前,你叫显示,首先,如上所述。

NOTE: If you simply use AlertDialog, then before getting the ListView, you call show, firstly, like explained above.

不过,如果你使用 DialogFragment onCreateDialog ,那么你得到的的ListView ,里面的在onStart

However, if you use DialogFragment and onCreateDialog, then you get the ListView, inside onStart.

这篇关于在对话框复选框自定义样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆