如何在Alert对话框android中更改内容文本的颜色

时间:2014-11-17 09:39:13

标签: android android-dialog

我被困住了,我不知道该怎么做。在我的应用程序中有一个带有列表视图的AlertDialog框,其中包含图像和文本但文本颜色为白色且不可见我想要更改ListView中文本的颜色。

请帮忙......

摘录如下:

  final String[] items = new String[]{"From Gallery", "From Camera"};
        final Integer[] icons = new Integer[]{R.drawable.camera_picker, R.drawable.gallery_picker};
        ListAdapter adapter = new CameraPickAdapter(StataComplaintActivity.this, items, icons);
        final AlertDialog.Builder builder = new AlertDialog.Builder(this, AlertDialog.THEME_HOLO_LIGHT);
        //AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.CustomAlertDialogTheme));

        builder.setTitle("Select Image From")
                .setAdapter(adapter, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int item) {
                        if (item == 0) {
                            loadImageFromGallery();
                        } else if (item == 1) {
                            takePictureIntent();
                        } else {
                        }
                    }
                }).show();

CameraPickAdapter.java

public class CameraPickAdapter extends ArrayAdapter<String> {

    private List<Integer> images;

    public CameraPickAdapter(Context context, List<String> items, List<Integer> images) {
        super(context, android.R.layout.select_dialog_item, items);
        this.images = images;
    }

    public CameraPickAdapter(Context context, String[] items, Integer[] images) {
        super(context, android.R.layout.select_dialog_item, items);
        this.images = Arrays.asList(images);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        TextView textView = (TextView) view.findViewById(android.R.id.text1);

        textView.setCompoundDrawablesWithIntrinsicBounds(images.get(position), 0, 0, 0);
        textView.setCompoundDrawablePadding(
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12, getContext().getResources().getDisplayMetrics()));
        return view;
    }

}

对话框如下:

enter image description here

3 个答案:

答案 0 :(得分:1)

尝试使用上下文引用将自定义或默认颜色设置为适配器中的TextView:

private Context context;

在构造函数中初始化上下文实例:

this.context = context;

现在使用上下文引用在getView()中设置自定义或默认文本颜色:

textView.setTextColor(context.getResources().getColor(R.color.custom_color_name)); // custom color

textView.setTextColor(context.getResources().getColor(android.R.color.black)); // default color

答案 1 :(得分:1)

按,

更改适配器代码
public class CameraPickAdapter extends ArrayAdapter<String> {

    private List<Integer> images;

    public CameraPickAdapter(Context context, List<String> items, List<Integer> images) {
        super(context, android.R.layout.select_dialog_item, items);
        this.images = images;
    }

    public CameraPickAdapter(Context context, String[] items, Integer[] images) {
        super(context, android.R.layout.select_dialog_item, items);
        this.images = Arrays.asList(images);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        TextView textView = (TextView) view.findViewById(android.R.id.text1);
        textView.setTextColor(Color.BLACK);
        textView.setCompoundDrawablesWithIntrinsicBounds(images.get(position), 0, 0, 0);
        textView.setCompoundDrawablePadding(
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12, getContext().getResources().getDisplayMetrics()));
        return view;
    }

}

答案 2 :(得分:0)

这是一个包含扩展ArrayAdapter的完整解决方案,允许使用图标。

请参阅位于http://developer.android.com/design/building-blocks/dialogs.htmlhttp://developer.android.com/design/style/iconography.html Iconogaphy和位于http://developer.android.com/design/downloads/index.html的IconPacks

上的对话框设计说明

请注意,这些尺寸相当于48 x 48 dp,这不是捆绑尺寸,因此您必须从下载中缩放自己的图标。

用法:

        @Override
    public void onClick(View v) {
        final String [] items = new String[] {"From Gallery", "From Camera"};
        final Integer[] icons = new Integer[] {R.drawable.dialog_gallery_icon, R.drawable.dialog_camera_icon};
        ListAdapter adapter = new ArrayAdapterWithIcon(getActivity(), items, icons);

        new AlertDialog.Builder(getActivity()).setTitle("Select Image")
            .setAdapter(adapter, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item ) {
                    Toast.makeText(getActivity(), "Item Selected: " + item, Toast.LENGTH_SHORT).show();
                }
        }).show();
    }

ArrayAdapterWithIcon.java

   public class ArrayAdapterWithIcon extends ArrayAdapter<String> {

   private List<Integer> images;

   public ArrayAdapterWithIcon(Context context, List<String> items, List<Integer> images) {
       super(context, android.R.layout.select_dialog_item, items);
       this.images = images;
   }

   public ArrayAdapterWithIcon(Context context, String[] items, Integer[] images) {
       super(context, android.R.layout.select_dialog_item, items);
       this.images = Arrays.asList(images);
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
       View view = super.getView(position, convertView, parent);
       TextView textView = (TextView) view.findViewById(android.R.id.text1);
       textView.setCompoundDrawablesWithIntrinsicBounds(images.get(position), 0, 0, 0);
       textView.setCompoundDrawablePadding(
               (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12,               getContext().getResources().getDisplayMetrics()));
       return view;
   }

   }