使用SpinnerAdapter自定义Android Spinner XML?

时间:2014-07-17 08:49:11

标签: java android xml spinner

我正在使用GitHub的源代码,我正在尝试编辑微调器的外观。对Spinner的唯一引用是在这个java文件中,我找不到可以为微调器设置自定义布局XML的位置。这是java文件:

package com.wallpaper.core;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.SpinnerAdapter;

import java.util.ArrayList;

public class Adapter extends BaseAdapter implements SpinnerAdapter {

private final String TAG = "Adapter";
private final OnGetViewListener mListener;
private ArrayList<?> mData;
private final LayoutInflater mInflater;

public static interface OnGetViewListener {
    public View getView (int position, View convertView, ViewGroup parent, LayoutInflater mInflater);
}

public Adapter (OnGetViewListener listener, Activity activity) {
    this.mData = new ArrayList<Object>();
    this.mInflater = LayoutInflater.from(activity);
    this.mListener = listener;
}

public Adapter (OnGetViewListener listener, Activity activity, ArrayList<?> data) {
    this.mData = (data == null) ? new ArrayList<Object>() : data;
    this.mInflater = LayoutInflater.from(activity);
    this.mListener = listener;
}

public ArrayList<?> getData () {
    return this.mData;
}

public void setData (ArrayList<?> data) {
    this.mData = data;
}

public void clearData () {
    this.mData.clear();
}

@Override
public int getCount () {
    if (mData == null)
        Log.d(TAG, "getCount() Data Set Is Null");
    return (mData != null) ? mData.size() : 0;
}

@Override
public Object getItem (int position) {
    if (mData == null)
        Log.d(TAG, "getItem(int position) Data Set Is Null");
    return (mData != null) ? mData.get(position) : null;
}

@Override
public long getItemId (int position) {
    if (mData == null)
        Log.d(TAG, "getItemId(int position) Data Set Is Null");
    return (mData != null) ? position : 0;
}

@Override
public View getView (int position, View convertView, ViewGroup parent) {
    return (mListener == null) ? new LinearLayout(mInflater.getContext()) : this.mListener.getView(position, convertView, parent, mInflater);
}

@Override
public View getDropDownView (int position, View convertView, ViewGroup parent) {
    return (mListener == null) ? new LinearLayout(mInflater.getContext()) : this.mListener.getView(position, convertView, parent, mInflater);
}

}

1 个答案:

答案 0 :(得分:0)

将您的微调器定义在您想要的位置

 <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:background="@drawable/spinner_button_dropdown"
        ></Spinner>

作为一个spinner_button_dropdown你可以使用这样的选择器(我知道你不是要求这个,但它显示了如何自定义外观):

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/spinner_pressed" android:state_pressed="true" />

    <item android:drawable="@drawable/spinner_normal" android:state_focused="true" />

    <item android:drawable="@drawable/spinner_normal" />

</selector>

在你的代码中你膨胀它

mySpinner = (Spinner)getView().findViewById(R.id.spinner);
mySpinner.setAdapter(myAdapter);
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

正如您所看到的,此示例中的微调器的外观不是从内部设置的,而是用户3249477从外部说的。希望它可以帮助你开始。