【问题标题】:Spinner's style is changed once i open it for the second一旦我第二次打开微调器,它的样式就会改变
【发布时间】:2018-02-20 20:32:29
【问题描述】:

我正在开发一个应用程序,我在其中使用微调器显示下拉列表。

我使用了自定义适配器,代码如下:

public class DemandeCongeAdapter extends BaseAdapter {
    public static final String TAG = "DemandeCongeAdapter";

    private final Context mContext;
    private List<DemandeCongeType> mData;
    protected LayoutInflater mInflater;

    public DemandeCongeAdapter(Context mContext, List<DemandeCongeType> mData) {
        this.mContext = mContext;
        this.mData = mData;
        this.mInflater = LayoutInflater.from(mContext);
    }

    static class ViewHolder {

        public ViewHolder(View v) {
            typedeDemandeName = (TextView) v.findViewById(R.id.type_demande_name);
        }

        protected final TextView typedeDemandeName;
    }


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

    @Override
    public DemandeCongeType getItem(int position) {
        return mData.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.layout_create_demande_conge_custom_spinner, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (position != 0) {
            DemandeCongeType item = getItem(position);

            String name = item.getTypdeDemandeName() == null ? "" : item.getTypdeDemandeName();
            viewHolder.typedeDemandeName.setText(name);
        }

        return convertView;
    }
}

活动代码如下:

private List<DemandeCongeType> congeTypes;
private DemandeCongeAdapter spinnerCongeTypeArrayAdapter;

congeTypes.addAll(new ArrayList<>((List<DemandeCongeType>) responseBody));
congeTypes.add(0, new DemandeCongeType());
spinnerCongeTypeArrayAdapter = new 
DemandeCongeAdapter(DemandeCongeNewActivity.this, congeTypes);
congeTypeSpinner.setAdapter(spinnerCongeTypeArrayAdapter);
congeTypeSpinner.setSelection(Adapter.NO_SELECTION, false);

一切正常,但是当我第一次选择微调器时,它会正确显示列表,如下所示:

当我在微调器外部单击并第二次单击微调器时,我得到了这样的结果:

所以我找不到问题,请如果有人可以帮助我。

这是 XML 代码:

<Spinner
    android:id="@+id/spinner_type_conge"
    fontPath="fonts/light.otf"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:background="@null"
    android:dropDownWidth="match_parent"
    android:ellipsize="end"
    android:lines="1"
    android:paddingRight="2dp"
    android:spinnerMode="dropdown"
    android:textSize="13sp" />

对于微调器项 xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/type_demande_name"
    fontPath="fonts/light.otf"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:lines="1"
    android:padding="10dp"
    android:textColor="@color/annuaire_hint_color"
    android:textSize="13sp" />

【问题讨论】:

    标签: java android xml styles spinner


    【解决方案1】:

    尝试这种方式,我在我的Application 中使用了它。

    XML 设计代码。

    Spinner 设置在你的Activity

    <Spinner
       android:id="@+id/sp_pen_Category"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
       android:background="@android:color/transparent"
       android:padding="@dimen/margin_10" />
    

    此文件XML 文件sp_list.xml

    <?xml version="1.0" encoding="utf-8"?>
    <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:ellipsize="marquee"
        android:padding="15dp"
        android:singleLine="true"
        android:textColor="@color/black"
        android:textSize="@dimen/sub_title">
    
    </CheckedTextView>
    

    此文件XML 文件sp_items.xml

    <?xml version="1.0" encoding="utf-8"?>
    <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:textColor="@color/black">
    
    </CheckedTextView>
    

    这是您填写spinner时的Java代码。

     ArrayAdapter<String> adp_sp = new ArrayAdapter<String>(Activity.this, R.layout.sp_items, arr_spinner);
        adp_sp.setDropDownViewResource(R.layout.sp_list);
        sp_pen_Category.setAdapter(adp_sp);
    

    【讨论】:

    • 谢谢@HardikParmar,我会试试的,但我需要覆盖setDropDownViewResource因为我有一个custom spinner
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-21
    • 2020-11-12
    • 1970-01-01
    相关资源
    最近更新 更多