Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在动态微调器中添加项目之间的间距_Java_Android - Fatal编程技术网

Java 如何在动态微调器中添加项目之间的间距

Java 如何在动态微调器中添加项目之间的间距,java,android,Java,Android,我已经创建了一个简单的动态微调器,我需要在每个项目之间添加间距和分隔线,我还需要知道如何添加更多属性,如项目文本颜色、项目文本大小……等等,通过在工具栏中创建此微调器的方式 这是我使用适配器的简单代码 Spinner navigationSpinner = new Spinner(getSupportActionBar().getThemedContext()); ArrayAdapter<String> adapter = new ArrayAdapter<String>

我已经创建了一个简单的动态微调器,我需要在每个项目之间添加间距和分隔线,我还需要知道如何添加更多属性,如项目文本颜色、项目文本大小……等等,通过在工具栏中创建此微调器的方式 这是我使用适配器的简单代码

Spinner navigationSpinner = new Spinner(getSupportActionBar().getThemedContext());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, Prests_Name);
navigationSpinner.setAdapter(adapter);
微调器导航微调器=新微调器(getSupportActionBar().getThemedContext());
ArrayAdapter=新的ArrayAdapter(这个,android.R.layout.simple\u微调器项目,Prests\u名称);
navigationSpinner.setAdapter(适配器);

我有同样的要求,所以我创建了带有一些额外参数的通用适配器类。因此,您可以检查此项并根据您的要求进行更改

SpinnerAdapter

public class SpinnerAdapter<T> extends ArrayAdapter<T> {

    private Context context;
    private ArrayList<T> values;
    private Typeface fontLight;
    private int color, layoutId, textViewId;
    //private int fontSize;

    private boolean IsSingleLine;
    private boolean IsFirstPositionSelectable, setTextSizeLimit = true;

    public SpinnerAdapter(Context context, int resource, int textViewResourceId,
        ArrayList<T> objects) {
        super(context, resource, textViewResourceId, objects);
        this.context = context;
        this.values = objects;
        this.layoutId = resource;
        this.textViewId = textViewResourceId;
    }

    public SpinnerAdapter(Context context, int resource, ArrayList<T> objects, int color,
        boolean isSingleLine) {
        super(context, resource, objects);

        this.context = context;
        this.values = objects;
        this.color = color;
        this.IsSingleLine = isSingleLine;
    }

    @Override public int getCount() {
        return values.size();
    }

    @Override public T getItem(int position) {
        return values.get(position);
    }

    @Override public long getItemId(int position) {
        return position;
    }

    public boolean isFirstPositionSelectable() {
        return IsFirstPositionSelectable;
    }

    public void setFirstPositionSelectable(boolean enable) {
        IsFirstPositionSelectable = enable;
    }

    public void setSetTextSizeLimit(boolean setTextSizeLimit) {
        this.setTextSizeLimit = setTextSizeLimit;
    }

    public Typeface getFontLight() {
        return fontLight;
    }

    public void setFontLight(Typeface fontLight) {
        this.fontLight = fontLight;
    }

    @Override public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);

            if (layoutId == 0) {
                convertView = inflater.inflate(android.R.layout.simple_spinner_item, parent, false);
            } else {
                convertView = inflater.inflate(layoutId, parent, false);
            }
        }

        TextView label;

        if (textViewId == 0) {
            label = (TextView) convertView.findViewById(android.R.id.text1);
        } else {
            label = (TextView) convertView.findViewById(textViewId);
        }

        if (setTextSizeLimit) {
            label.setFilters(new InputFilter[] { new InputFilter.LengthFilter(2) });
        }

        label.setSingleLine(IsSingleLine);
        label.setPaddingRelative(25, 15, 25, 15);

        if (fontLight != null)
            label.setTypeface(getFontLight());

        label.setText(values.toArray(new Object[values.size()])[position].toString());

        return label;
    }

    @Override public View getDropDownView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);

            if (layoutId == 0) {
                convertView = inflater.inflate(android.R.layout.simple_spinner_item, parent, false);
            } else {
                convertView = inflater.inflate(layoutId, parent, false);
            }
        }

        TextView label;

        if (textViewId == 0) {
            label = (TextView) convertView.findViewById(android.R.id.text1);
        } else {
            label = (TextView) convertView.findViewById(textViewId);
        }


        //label.setPadding(30, 10, 10, 30);
        //label.setTextSize(context.getResources().getDimension(R.dimen.text_size_small));
        if (fontLight != null)
            label.setTypeface(fontLight);

        label.setSingleLine(IsSingleLine);

        label.setText(values.toArray(new Object[values.size()])[position].toString());

        return label;
    }
}
公共类SpinnerAdapter扩展了ArrayAdapter{
私人语境;
私有数组列表值;
私人字体;
私有int颜色、layoutId、textViewId;
//私用字号;
私有布尔线性;
私有布尔值为firstPositionSelectable,setTextSizeLimit=true;
公共SpinnerAdapter(上下文上下文、int-resource、int-textViewResourceId、,
ArrayList对象){
超级(上下文、资源、textViewResourceId、对象);
this.context=上下文;
这个值=对象;
this.layoutId=资源;
this.textViewId=textViewResourceId;
}
公共SpinnerAdapter(上下文上下文、int资源、ArrayList对象、int颜色、,
布尔值(直线){
超级(上下文、资源、对象);
this.context=上下文;
这个值=对象;
这个颜色=颜色;
this.IsSingleLine=IsSingleLine;
}
@重写公共int getCount(){
返回值。size();
}
@覆盖公共T getItem(内部位置){
返回值.get(位置);
}
@覆盖公共长getItemId(int位置){
返回位置;
}
公共布尔值isFirstPositionSelectable(){
返回是可选择的;
}
公共void setFirstPositionSelective(布尔启用){
IsFirstPositionSelective=启用;
}
public void setSetTextSizeLimit(布尔setTextSizeLimit){
this.setTextSizeLimit=setTextSizeLimit;
}
公共字体getFontLight(){
返回灯;
}
公共void setFontLight(字体fontLight){
this.fontLight=fontLight;
}
@覆盖公共视图getView(int位置、视图转换视图、视图组父视图){
if(convertView==null){
LayoutFlater充气机=LayoutFlater.from(上下文);
如果(layoutId==0){
convertView=充气机。充气(android.R.layout.simple\u微调器\u项,父项,false);
}否则{
convertView=充气机。充气(layoutId,父项,false);
}
}
文本视图标签;
如果(textViewId==0){
label=(TextView)convertView.findViewById(android.R.id.text1);
}否则{
label=(TextView)convertView.findViewById(textViewId);
}
if(setTextSizeLimit){
label.setFilters(newInputFilter[]{newInputFilter.LengthFilter(2)});
}
标签。设置单线(IsSingleLine);
label.setPaddingRelative(25,15,25,15);
如果(fontLight!=null)
label.setTypeface(getFontLight());
label.setText(values.toArray(新对象[values.size()])[position].toString());
退货标签;
}
@覆盖公共视图getDropDownView(int位置、视图转换视图、视图组父视图){
if(convertView==null){
LayoutFlater充气机=LayoutFlater.from(上下文);
如果(layoutId==0){
convertView=充气机。充气(android.R.layout.simple\u微调器\u项,父项,false);
}否则{
convertView=充气机。充气(layoutId,父项,false);
}
}
文本视图标签;
如果(textViewId==0){
label=(TextView)convertView.findViewById(android.R.id.text1);
}否则{
label=(TextView)convertView.findViewById(textViewId);
}
//标签。设置填充(30,10,10,30);
//label.setTextSize(context.getResources().getdimen(R.dimen.text_size_small));
如果(fontLight!=null)
标签。设置字体(fontLight);
标签。设置单线(IsSingleLine);
label.setText(values.toArray(新对象[values.size()])[position].toString());
退货标签;
}
}

您的意思是我应该创建一个带有适配器的自定义微调器@androidnoobdev@Dev.7arooney不使用自定义适配器的默认微调器,在该适配器中执行所有自定义操作。非常感谢您的帮助,我将尝试此操作