美文网首页
自定义Spinner 下拉选择框

自定义Spinner 下拉选择框

作者: rowliner | 来源:发表于2017-05-17 10:58 被阅读0次

google原生的Spinner很难满足我们的项目需求,比如设置背景色,这时候我们就需要自定义Spinner

1 采用TextView加 popupwindow实现自定义Spinner

2 效果如下

带有选中效果

3 自定义spinnerview继承 textview   代码如下:


packagecom.teswell.textcar.widget;

importandroid.content.Context;

importandroid.content.res.TypedArray;

importandroid.graphics.Color;

importandroid.graphics.drawable.ColorDrawable;

importandroid.graphics.drawable.Drawable;

importandroid.util.AttributeSet;

importandroid.view.Gravity;

importandroid.view.View;

importandroid.view.ViewGroup;

importandroid.widget.LinearLayout;

importandroid.widget.PopupWindow;

importandroid.widget.TextView;

importcom.teswell.textcar.R;

importjava.util.List;

/**

* Created by lolin on 2017/5/16.

* 自带下拉选择框

*/

public classSpinnerViewextendsTextViewimplementsView.OnClickListener {

privatePopupWindowpopupWindow;

privateString[]title;

privateSpinnerViewListenerlistener;

publicSpinnerView(Context context) {

super(context);

}

publicSpinnerView(Context context,AttributeSet attrs) {

super(context,attrs);

init(attrs);

}

publicSpinnerView(Context context,AttributeSet attrs, intdefStyleAttr) {

super(context,attrs,defStyleAttr);

init(attrs);

}

public voidsetData(List list) {

title= list.toArray(newString[list.size()]);

invalidate();

}

public voidsetSpinnerViewListener(SpinnerViewListener listener) {

this.listener= listener;

}

private voidinit(AttributeSet attrs) {

TypedArray ta = getContext().obtainStyledAttributes(attrs,R.styleable.TitleSeletorView);

intarrayId = ta.getResourceId(R.styleable.TitleSeletorView_text,0);

if(arrayId !=0) {

title= getResources().getStringArray(arrayId);

}

setOnClickListener(newOnClickListener() {

@Override

public voidonClick(View v) {

showPopupWindow(v);

}

});

setBackgroundColor(getResources().getColor(R.color.home_bg));

setRightDrawable(R.drawable.unselect_popupwindow);

}

private voidsetRightDrawable(intdrawableRes) {

Drawable nav_up = getResources().getDrawable(drawableRes);

nav_up.setBounds(0,0,nav_up.getMinimumWidth(),nav_up.getMinimumHeight());

setCompoundDrawables(null, null,nav_up, null);

}

@Override

public voidonClick(View v) {

popupWindow.dismiss();

setText(((TextView) v).getText());

if(listener!=null)

listener.onTextChager((Integer) v.getTag(),title[(Integer) v.getTag()]);

}

private voidshowPopupWindow(View parent) {

if(title==null||title.length==0)return;

if(popupWindow==null) {

LinearLayout linearLayout =newLinearLayout(getContext());

ViewGroup.LayoutParams lp =newViewGroup.LayoutParams(getWidth(),ViewGroup.LayoutParams.WRAP_CONTENT);

linearLayout.setOrientation(LinearLayout.VERTICAL);

linearLayout.setLayoutParams(lp);

for(inti =0;i

TextView textView =newTextView(getContext());

LinearLayout.LayoutParams tv =newLinearLayout.LayoutParams(getWidth(),34);

tv.setMargins(0,1,0,0);

textView.setTextSize(20f);

textView.setTextColor(Color.WHITE);

textView.setGravity(Gravity.CENTER);

textView.setLayoutParams(tv);

textView.setOnClickListener(this);

textView.setText(title[i]);

textView.setTag(i);

textView.setBackgroundColor(Color.parseColor("#161616"));

linearLayout.addView(textView);

}

popupWindow=newPopupWindow(linearLayout,getWidth(),ViewGroup.LayoutParams.WRAP_CONTENT);

}

//  2017/5/16 设置动画

//        popupWindow.setAnimationStyle(R.style.popup_window_anim);

// 2017/5/16 设置背景颜色

popupWindow.setBackgroundDrawable(newColorDrawable(Color.parseColor("#191919")));

// 2017/5/16 设置可以获取焦点

popupWindow.setFocusable(true);

// 2017/5/16 设置可以触摸弹出框以外的区域

popupWindow.setOutsideTouchable(true);

//设置点击区域外消失

popupWindow.setTouchable(true);

popupWindow.setOnDismissListener(newPopupWindow.OnDismissListener() {

@Override

public voidonDismiss() {

setRightDrawable(R.drawable.unselect_popupwindow);

}

});

// 更新popupwindow的状态

//        popupWindow.update();

// 2017/5/16 以下拉的方式显示,并且可以设置显示的位置

popupWindow.showAsDropDown(parent,0,0);

setRightDrawable(R.drawable.select_popupwindow);

}

public interfaceSpinnerViewListener {

voidonTextChager(intproint,String text);

}

}


4 自定义Spinnerview继承的是TextView所有用法跟TextView一样

我这里给他增加了自定义属性,不需要的同学可以去掉

相关文章

网友评论

      本文标题:自定义Spinner 下拉选择框

      本文链接:https://www.haomeiwen.com/subject/bonrxxtx.html