Android Spinner项目作为网格

时间:2013-11-14 08:10:02

标签: android grid android-spinner

默认情况下,单击微调器时,项目显示为列表。我想改变显示为网格的项目。 我怎样才能做到这一点? 我只需要一些方向。

由于 更新: 这是我的代码。我的微调器下拉显示为多行网格。我已经附上了此时它的样子。我也无法选择值。

enter image description here

    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>

1 个答案:

答案 0 :(得分:4)

您需要覆盖getDropDownView的{​​{1}}方法才能显示Spinner

要从GridView中选择值,请使用Spinner上的OnItemClickListener获取回调。例如:

GridView