我知道如何自定义微调器。就我而言,它有点不同。我现在无法修改我的代码。通常,我可以创建可从我的应用程序中的所有活动访问的微调器。 我试图将文本视图中的图标放在微调器下拉列表中。 这是我的代码;
Spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<Spinner
android:id="@+id/spinner"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:spinnerMode="dialog"
android:prompt="@string/language_prompt" />
spinner_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:addStatesFromChildren="true"
android:gravity="center_horizontal"
android:padding="5dp"
android:textColor="#FFFFFF"
android:textSize="17sp" >
</TextView>
<!-- Here I cannot use Relative layout. If I use it gives error like [java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView]. Without layout I cannot implement ImageView. I am stuck here.
我的活动课程;
public class Base_Activity extends Activity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
//int flags = R.array.flags;
final Spinner spinner = (Spinner) menu.getItem(0).getActionView()
.findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.languages, R.layout.spinner_row);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
我想要的是自定义适配器,以便我可以将文本视图放在微调器列表中。我无法以正常的方式检查我在许多教程中查看过的内容。我认为这是因为我在onCreateOptionsMenu()构造函数中创建了微调器。
答案 0 :(得分:1)
你可以看看这个tutorial on AndroidHive
http://www.androidhive.info/2013/11/android-working-with-action-bar/
代码段
// Spinner title navigation data
navSpinner = new ArrayList<SpinnerNavItem>();
navSpinner.add(new SpinnerNavItem("Local", R.drawable.ic_location));
navSpinner
.add(new SpinnerNavItem("My Places", R.drawable.ic_my_places));
navSpinner.add(new SpinnerNavItem("Checkins", R.drawable.ic_checkin));
navSpinner.add(new SpinnerNavItem("Latitude", R.drawable.ic_latitude));
// title drop down adapter
adapter = new TitleNavigationAdapter(getApplicationContext(),
navSpinner);
来自getView()
的 getDropDownView()
和TitleNavigationAdapter
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_item_title_navigation, null);
}
imgIcon = (ImageView) convertView.findViewById(R.id.imgIcon);
txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
imgIcon.setImageResource(spinnerNavItem.get(position).getIcon());
imgIcon.setVisibility(View.GONE);
txtTitle.setText(spinnerNavItem.get(position).getTitle());
return convertView;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_item_title_navigation, null);
}
imgIcon = (ImageView) convertView.findViewById(R.id.imgIcon);
txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
imgIcon.setImageResource(spinnerNavItem.get(position).getIcon());
txtTitle.setText(spinnerNavItem.get(position).getTitle());
return convertView;
}
下载该代码,并且很高兴