5

默认情况下,单击 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;
        }   
    }

主要的.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>

行.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>
4

1 回答 1

4

您需要覆盖getDropDownView您的方法Spinner以显示GridView.

为了从您的 中选择值,请Spinner使用OnItemClickListener您的GridView来获取回调。例如:

label.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
        Toast.makeText(getContext(), "" + pos, Toast.LENGTH_SHORT).show();
    }
});
于 2013-11-14T08:20:18.100 回答