Android Spinner项目作为网格
作者:互联网
默认情况下,单击“微调器”时,项目显示为列表.我想更改显示为网格的项目.
我怎样才能做到这一点?
我只需要一些指示.
谢谢
更新:
这是我的代码.我的微调框下拉列表显示为多行网格.我也无法选择值.
package com.example.com.android.spinner;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
public class AndroidCustomSpinner extends Activity {
String[] DayOfWeek = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
mySpinner.setAdapter(new MyCustomAdapter(AndroidCustomSpinner.this, R.layout.row, DayOfWeek));
}
public class MyCustomAdapter extends ArrayAdapter<String>{
private Context context;
public MyCustomAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
this.context=context;
// TODO Auto-generated constructor stub
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView label=(TextView)convertView;
if (convertView==null) {
convertView=new TextView(context);
label=(TextView)convertView;
}
label.setText(DayOfWeek[position]);
return(convertView);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//return super.getView(position, convertView, parent);
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.row, parent, false);
GridView label=(GridView)row.findViewById(R.id.gridView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(AndroidCustomSpinner.this,android.R.layout.simple_list_item_1,DayOfWeek);
label.setAdapter(adapter);
return row;
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<GridView
android:id="@+id/gridView1"
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="50dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</GridView>
</LinearLayout>
解决方法:
您需要重写Spinner的getDropDownView方法以显示GridView.
为了从Spinner中选择值,请在GridView上使用OnItemClickListener来获取回调.例如:
label.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
Toast.makeText(getContext(), "" + pos, Toast.LENGTH_SHORT).show();
}
});
标签:android-spinner,grid,android 来源: https://codeday.me/bug/20191030/1966074.html