本文介绍了使用自定义适配器时,ListView不会使用主题设置样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个片段的minidemo.一个直接显示两个 TextView 和一个 editview ,第二个显示与具有自定义适配器的列表中的行相同的布局,第三个显示与标准适配器(simple_list_item_1)的纯列表.

该活动使用标准主题( Theme.AppCompat.Light.DarkActionBar ),但我尚未对其进行修改.

我希望所有三个块的样式都相似.相反,带有自定义适配器的第二个块似乎根本没有根据灯光主题设置样式.(请参阅本文末尾的屏幕截图)

理想的是,我希望自定义列表的行的样式像第一个块一样.或者,我想应用第三个代码块的样式.但是,所有字体和背景似乎都有些偏色,我不确定如何找出当前用作主题的一部分.因此,不能通过手动分配文本颜色,大小,背景等来固定中间块的样式.

有关整个演示,请参阅

诀窍是在创建适配器时删除 getActivityContext()并直接传递活动对象.悲伤但有效.

I have minidemo with three fragments. One displays two TextView and an editview directly, the next treat the same layout as rows in a list with custom adapter, the third displays a plain list with standard adapter (simple_list_item_1)

The activity uses a standard theme (Theme.AppCompat.Light.DarkActionBar), which I have not modified.

I would have expected all three blocks to be styled similarly. Instead, the second block with the custom adapter does not seem to get styled according to the light theme at all. (See screenshot at end of this post)

Ideal,ly I'd like the rows of the custom list to be styled like the first block. Alternatively, I'd like to get the style of the third block applied. However, all of the fonts and backgrounds seem to be some off color and I am not sure how to find out what is currently used as part of the theme. Therefore, fixing the middle block's style by manually assigning text colors, sizes, backgrounds, etc. is not an option.

For the whole demo please refer to MiniDemo

This is the item_row.xml layout file (The top level relative layout tag seems to be stripped by the editor. For the full layout please go to gitlab)

<TextView android:text="large"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:textAppearanceLarge"
    android:id="@+id/text1_field" />

<TextView android:text="large inverse"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text2_field"
    android:textAppearance="?android:attr/textAppearanceLargeInverse"
    android:layout_centerHorizontal="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="5"
    android:id="@+id/edit_field"
    android:layout_alignParentRight="true" />
</RelativeLayout>

The custom fragment. Again, the full version is on gitlab

public class CustomItemFragment extends ListFragment {
String[] DATA = new String[] {"foo1", "foo2", "foo3"};

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ItemAdapter adapter = new ItemAdapter(this.getActivity().getApplicationContext(), DATA);
    setListAdapter(adapter);
}

}`

The adapter (gitlab):

public class ItemAdapter extends BaseAdapter {
private static final String TAG = ItemAdapter.class.getSimpleName();

private final Context mContext;
private final String[] mData;

public ItemAdapter(Context mContext, String[] data) {
    this.mContext = mContext;
    this.mData = data;
}

@Override
public int getCount() {
    return mData.length;
}

@Override
public Object getItem(int position) {
    return mData[position];
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) LayoutInflater.from(mContext);
    View rowView = inflater.inflate(R.layout.item_row, parent, false);

    TextView text1View = (TextView) rowView.findViewById(R.id.text1_field);
    TextView text2View = (TextView) rowView.findViewById(R.id.text2_field);
    EditText editText = (EditText) rowView.findViewById(R.id.edit_field);

    text1View.setText(mData[position]);
    text2View.setText(mData[position]);
    editText.setText(Integer.toString(position));

    return rowView;
}

And then, here is a sceenshot to demo the effect.

解决方案

Arghh, I just found the answer, with a hint from Use android.R.layout.simple_list_item_1 with a light theme

The trick is to remove getActivityContext() and to pass in the activity object directly when creating the adapter. Sad but effective.

这篇关于使用自定义适配器时,ListView不会使用主题设置样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:19