2014-11-17 3 views
1

Я застрял, и я не понимаю, как это сделать. В моих приложениях есть окно AlertDialog Box со списком, содержащее изображение и текст, но текст становится белым по цвету и не отображается. Я хочу изменить цвет текста в ListView.Как изменить цвет текста контента в диалоговом окне «Предупреждение» android

Пожалуйста, помогите ...

отрывки являются следующие:

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

+0

вы можете поместить код CameraPickAdapter? –

+0

@MoradiyaAkash Я добавил код для CameraPickAdapter – anand

ответ

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

Изменение кода адаптера с помощью,

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

} 
0

Здесь представляет собой комплексное решение с расширенным ArrayAdapter, что позволяет иконки.

См дизайн примечания к диалогам в http://developer.android.com/design/building-blocks/dialogs.html Iconogaphy в http://developer.android.com/design/style/iconography.html и IconPacks в http://developer.android.com/design/downloads/index.html

Обратите внимание, что размер этих выглядит довольно хорошо на 48 х 48 дп, которая не является в комплекте размер, так что вы будете иметь масштабировать свой собственный значок из загрузок.

ПРИМЕНЕНИЕ:

 @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; 
    } 

    } 
Смежные вопросы