Spinner(下拉)项目与Spinner大小相同

时间:2014-08-22 14:49:37

标签: android

我可以制作我的物品的大小,尤其是与旋转器相同大小的dropdownitems吗?它们目前有点小。我希望他们能够填满整个Spinner。 任何人都可以帮助我吗?

编辑:

它看起来像这样: http://image-upload.de/image/vjYswg/cbc92fd3e9.jpg

我想要这个: http://image-upload.de/image/JdVZ5z/1f73d79751.jpg

哦,我必须使用API​​ 14,所以我无法设置dropdownwidth

我已将dropdownviewresource设置为以下xml:

<?xml version="1.0" encoding="utf-8"?>
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape = "rectangle">
    <solid
        android:color="#666666" />
    <corners
        android:radius="3dp" />
</shape>

编辑:sry,这是我的微调器布局,对于我使用的dropdown_items

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="43dp"
    android:orientation="horizontal" >
    <ImageView
        android:layout_height="30dp"
        android:layout_width="30dp"
        android:layout_marginRight="5dp"
        android:layout_gravity="center_vertical"
        android:contentDescription="@layout/spinner_item"
        android:src="@drawable/circledsync" />
    <de.util.FontDropDownTextView
        android:id = "@+id/spinner_dropdown_item_textView"
        style="?android:attr/spinnerDropDownItemStyle"
        android:singleLine="true"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="0.90"
        android:gravity="center_vertical"
        android:textColor="@color/spinner_text" />
</LinearLayout>

我还使用自定义数组适配器:

public class MyArrayAdapter extends ArrayAdapter<String> {

    private int myTextId = 0; 
    private int myDropDownResource;
    private int myDropDownTextId;
    private int myResource;

    private LayoutInflater myInflater;

    public MyArrayAdapter(Context context, int textViewResourceId, List<String> names) {
        super(context, textViewResourceId, names);  
        myTextId = myDropDownTextId = textViewResourceId;// remember it, so we can find the real TextView
        myInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public MyArrayAdapter(Context context, int layoutId, int textViewResourceId, List<String> names) {
        super(context, layoutId, textViewResourceId, names);
        myTextId = myDropDownTextId = textViewResourceId;// remember it, so we can find the real TextView
        myResource = myDropDownResource = layoutId;
        myInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void setDropDownViewResource(int resource) {
        super.setDropDownViewResource(resource);
        myDropDownResource = resource;
    }

    public void setDropDownViewResource(int resource, int textId) {
        super.setDropDownViewResource(resource);
        myDropDownResource = resource;
        myDropDownTextId = textId;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
            View cell = MyGetView(position, convertView, parent, myResource, myTextId);
            setFontForChild(cell);
            return cell;
        }

    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        View cell = MyGetView(position, convertView, parent, myDropDownResource, myDropDownTextId);
            setFontForChild(cell);
            return cell;
        }

    private void setFontForChild(View layoutCell)
    {
        View realTextView = layoutCell.findViewById(myTextId);
        if(realTextView instanceof TextView)
        {
            Typeface tf = Typeface.createFromAsset(
                getContext().getAssets(),"fonts/calibri.otf");
            ((TextView)realTextView).setTypeface(tf);   
        }
        //else realTextView is null or is not a TextView.
    }

    private View MyGetView(int position, View convertView, ViewGroup parent,
            int resource, int fieldId) {
        View view;
        TextView text;

        if (convertView == null) {
            int res = (resource==0)? fieldId : resource;
            view = myInflater.inflate(res, parent, false);
        } else {
            view = convertView;
        }

        try {
            if (fieldId == 0) {
                //  If no custom field is assigned, assume the whole resource is a TextView
                text = (TextView) view;
            } else {
                //  Otherwise, find the TextView field within the layout
                text = (TextView) view.findViewById(fieldId);
            }
        } catch (ClassCastException e) {
            Log.e("ArrayAdapter", "You must supply a resource ID for a TextView");
            throw new IllegalStateException(
                    "ArrayAdapter requires the resource ID to be a TextView", e);
        }

        String item = getItem(position);
        if (item instanceof CharSequence) {
            text.setText((CharSequence)item);
        } else {
            text.setText(item);
        }

        return view;        
    }

5 个答案:

答案 0 :(得分:3)

我遇到了同样的问题。我改变了我的Spinner的风格:

<item name="android:paddingLeft">0dp</item>
<item name="android:paddingRight">0dp</item>

它解决了我的问题。

答案 1 :(得分:1)

在java代码中尝试:

spinner.setPopupBackgroundResource(R.drawable.spinner_style);

在您的spinner_style.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#ffffff" />
</shape>

答案 2 :(得分:0)

添加如下调用:

Spinner spinner = (Spinner)findViewById(...);
spinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

答案 3 :(得分:0)

xml文件中尝试此操作: -

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:textSize="14pt"

根据您的要求使用尺寸。

答案 4 :(得分:0)

尝试使用自定义Spinner,它将解决您的问题。 样品:

启动:

spinner.setAdapter(new MyCustomAdapter(AssessmentActivity.this,
                            R.layout.spinner_test, spinnerData));

在自定义适配器中:

public class MyCustomAdapter extends ArrayAdapter<String>{
                List<String>draft_filter_array_ = new ArrayList<String>();


            public MyCustomAdapter(Context context, int textViewResourceId,
                    List<String> draft_filter_array) {
                super(context, textViewResourceId, draft_filter_array);
                draft_filter_array_ = draft_filter_array;

            }

                @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
                return getCustomView(position, convertView, parent);
                }

                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.spinner_test, parent, false);
                TextView label=(TextView)row.findViewById(R.id.weekofday);
                label.setText(draft_filter_array_.get(position));


                return row;
                }
                }

在你的spinner_test.xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<TextView
        android:id="@+id/weekofday"
        android:layout_width="match_parent"
        android:layout_height="42dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Sunday"
        android:textColor="#000000" 
        android:textSize="18sp"
        />
</LinearLayout>