为了要改变提示,您需要使用与@ aswin-kumar建议的类似方法,但是对于第一个元素的下拉列表使用单独的样式:

首先,扩展ArrayAdapter并称之为CustomArrayAdapter: package com.example.project;

package com.example.project;

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.LinearLayout;

import android.widget.TextView;

import java.util.List;

// I extend this using ArrayAdapter, but you can use anything as the type

// parameter, or parameterize it as necessary.

public class CustomArrayAdapter extends ArrayAdapter {

private String title;

public CustomArrayAdapter(Context aContext, int aTextViewResource, List aOptions, String title) {

super(aContext, aTextViewResource, aOptions);

this.title = title;

}

@Override

public View getDropDownView(int aPosition, View aConvertView, ViewGroup aParent) {

LinearLayout v = null;

if (aPosition == 0) {

LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

v = (LinearLayout) inflater.inflate(R.layout.dropdown_header, aParent, false);

TextView tv = (TextView) v.findViewById(R.id.dropdown_item);

tv.setText(getItem(aPosition));

} else {

LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

v = (LinearLayout) inflater.inflate(R.layout.dropdown_item, aParent, false);

TextView tv = (TextView) v.findViewById(R.id.dropdown_item);

tv.setText(getItem(aPosition));

tv.setHeight((int) (tv.getTextSize() * 2));

}

return v;

}

@Override

public int getCount() {

return super.getCount() + 1;

}

@Override

public String getItem(int position) {

if (position == 0) {

return title;

}

return super.getItem(position - 1);

}

@Override

public boolean isEnabled(int position) {

return position != 0;

}

}

现在,请您dropdown_header.xml以下布局:

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="vertical">

android:id="@+id/dropdown_item"

android:textColor="@android:color/black"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:paddingTop="16dp"

android:paddingBottom="16dp"

android:paddingLeft="16dp"

android:gravity="left|center_vertical"

android:textSize="9pt" />

android:id="@+id/separator"

android:layout_height="4dp"

android:layout_width="match_parent"

android:background="@android:color/black"

android:orientation="vertical"/>

表单视图布局文件(form_view_layout。XML):

android:id="@+id/spinner_dialog_root"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="9pt" />

的微调布局文件(spinner_layout.xml):

android:id="@+id/spinner"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:background="@android:color/transparent"/>

现在,附加新的适配器与Spinner:

CustomArrayAdapter adapter = new CustomArrayAdapter(aActivity, R.layout.form_view_layout, aModel.getEnumerableOptions(), aModel.getTitle());

View parentView = aActivity.getLayoutInflater().inflate(R.layout.spinner_layout, aParent, false);

spinner = (Spinner) parentView.findViewById(R.id.spinner);

spinner.setAdapter(adapter);

最后,添加你想要的字符串显示到您的微调适配器,adapter,并设计适当的文件,你应该很好去。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐