How to override the style of android.R.layout.simp

2020-07-16 02:32发布

I use adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, cur, cols, views) to create a multiple choice control, but I'm not satisfied of the style of the textview in the multiple choice control, so I have to use the following code to create a new layout of a multiple choice control. It works well, but I don't think it's a good way, is there any good code? Thanks!

adapter = new SimpleCursorAdapter(this, R.layout.mysimple_list_item_multiple_choice, cur, cols, views);

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
2条回答
够拽才男人
2楼-- · 2020-07-16 03:01

You need to extend BaseAdapter to create a custom Adapter.With A custom adapter each row of listview uses a xml layout and hence you can create a custom layout for rows .

Example code of custom adapter :

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

}

Setting adapter:

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

Links of examples:

example 1 example 2

R.layout.list_view_item is the custom xml for your list view ,in which you can add desired views .

查看更多
Viruses.
3楼-- · 2020-07-16 03:08

Simple.

Use <include>.

Create a new XML layout.

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

You can override all the layout parameters (any android:layout_* attributes) of the included layout's root view by specifying them in the tag.

查看更多
登录 后发表回答