【问题标题】:How to override the style of android.R.layout.simple_list_item_multiple_choice?如何覆盖android.R.layout.simple_list_item_multiple_choice的样式?
【发布时间】:2013-06-05 15:08:43
【问题描述】:

我使用 adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, cur, cols, views) 创建一个多选控件, 但是我对多选控件中textview的样式不满意,所以我不得不使用下面的代码来创建多选控件的新布局。 效果不错,但我觉得不是什么好办法,有什么好的代码吗?谢谢!

适配器 = 新的 SimpleCursorAdapter(这个, R.layout.mysimple_list_item_multiple_choice, cur, cols, 视图);

lv.setAdapter(adapter);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

mysimple_list_item_multiple_choice.xml

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:paddingLeft="6dip"
    android:paddingRight="6dip"
    android:ellipsize="end"
    android:singleLine="true"

/>

【问题讨论】:

  • 通过android中的'custome adapters'

标签: android


【解决方案1】:

简单。

使用&lt;include&gt;

创建一个新的 XML 布局。

     <include android:id=”@+android:id/simple_list_item_multiple_choice”
     android:layout_width=”match_parent”
     android:layout_height=”match_parent”
     layout=”@layout/title”/>

您可以通过在标签中指定它们来覆盖包含布局的根视图的所有布局参数(任何android:layout_* 属性)。

【讨论】:

    【解决方案2】:

    您需要扩展BaseAdapter 以创建自定义适配器。使用自定义适配器,列表视图的每一行都使用 xml 布局,因此您可以为行创建自定义布局。

    自定义适配器示例代码:

    public class ImageAdapter extends BaseAdapter {
        private Context context;
        private final String[] StrValues;
    
        public ImageAdapter(Context context, String[] StrValues) {
            this.context = context;
            this.StrValues = StrValues;
        }
    
        // getView that displays the data at the specified position in the data set.
        public View getView(int position, View convertView, ViewGroup parent) {
            // create a new LayoutInflater
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            View view;
            view = null;
            convertView = null;// avoids recycling of list view
            if (convertView == null) {
    
                view = new View(context);
                // inflating grid view item
                view = inflater.inflate(R.layout.list_view_item, null);
    
                // set value into textview
                TextView textView = (TextView) view
                        .findViewById(R.id.list_item_label);
                textView.setText(StrValues[position]);
    
            }
    
            return view;
        }
    
        // Total number of items contained within the adapter
        @Override
        public int getCount() {
            return StrValues.length;
        }
    
        @Override
        public Object getItem(int position) {
            return null;
        }
    
        @Override
        public long getItemId(int position) {
            return 0;
        }
    
    }
    

    设置适配器:

     listView = (ListView) findViewById(R.id.listView1);
        // setting adapter on listview
        listView.setAdapter(new ImageAdapter(this, StrValues));
    

    示例链接:

    example 1 example 2

    R.layout.list_view_item 是您的列表视图的自定义 xml,您可以在其中添加所需的视图。

    【讨论】:

    • 如果您必须做一些默认情况下没有提供的事情,那么没有其他选择。AFAIK只能通过自定义适配器来完成
    • 这是一种复杂的方式来完成如此简单的任务恕我直言。
    猜你喜欢
    • 2015-06-22
    • 2019-08-05
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    相关资源
    最近更新 更多