安卓开发之自绘spinner控件

一个简单的安卓spinner控件自绘。外观可行,但实用性不怎么样。

控件分为两个部分:一个textview和一个popupwindow

textview自定义边框形状、边线,并在右边添加“﹀“”︿”图像,单击textview时弹出popupwindow



代码:

主活动中:

public class MainActivity extends Activity 
{
	private SpinerPopWindow<String> mSpinerPopWindow;
	private List<String> list = new ArrayList<String>();
	private TextView tvValue;


	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);

list.add("test1:");
list.add("test2:");
list.add("test3:");
list.add("test4:");
list.add("test5:");
list.add("test6:");



		tvValue = (TextView) findViewById(R.id.tv_value);
		//显示PopupWindow
		tvValue.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) 
			{
				switch (v.getId()) 
				{
				case R.id.tv_value:
					mSpinerPopWindow.setWidth(tvValue.getWidth());
					mSpinerPopWindow.showAsDropDown(tvValue);
					setTextImage(R.drawable.icon_up);
					break;
				}
			}
		});
		//popupwindow显示的ListView的item点击事件
		mSpinerPopWindow = new SpinerPopWindow<String>(this, list,new OnItemClickListener() 
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,long id) 
			{
				mSpinerPopWindow.dismiss();
				tvValue.setText(list.get(position));
			}
		});
		//监听popupwindow取消
		mSpinerPopWindow.setOnDismissListener(new OnDismissListener()
		{
			@Override
			public void onDismiss() 
			{
				setTextImage(R.drawable.icon_down);
			}
		});
	}
	




	//设置右端图像
	private void setTextImage(int resId) 
	{
		Drawable drawable = getResources().getDrawable(resId);
		drawable.setBounds(0, 0, drawable.getMinimumWidth(),drawable.getMinimumHeight());// 必须设置图片大小,否则不显示
		tvValue.setCompoundDrawables(null, null, drawable, null);
	}
}

PopupWindow代码:

@SuppressWarnings("unused")
@SuppressLint("InflateParams") 
public class SpinerPopWindow<T> extends PopupWindow 
{
	private LayoutInflater inflater;
	private ListView mListView;
	private List<T> list;	
	private MyAdapter  mAdapter;
	
	public SpinerPopWindow(Context context,List<T> list,OnItemClickListener clickListener) 
	{
		super(context);
		inflater=LayoutInflater.from(context);
		this.list=list;
		init(clickListener);
	}
	
	
	private void init(OnItemClickListener clickListener)
	{
		View view = inflater.inflate(R.layout.spiner_window_layout, null);
		setContentView(view);		
		setWidth(LayoutParams.WRAP_CONTENT);
		setHeight(LayoutParams.WRAP_CONTENT);
		setFocusable(true);
    		ColorDrawable dw = new ColorDrawable(0x00);
		setBackgroundDrawable(dw);
		mListView = (ListView) view.findViewById(R.id.listview);
		mListView.setAdapter(mAdapter=new MyAdapter());
		mListView.setOnItemClickListener(clickListener);
	}
	
	private class MyAdapter extends BaseAdapter
	{
		@Override
		public int getCount() 
		{
			return list.size();
		}

		@Override
		public Object getItem(int position) 
		{
			return list.get(position);
		}

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

		@SuppressWarnings("unchecked")
		@Override
		public View getView(int position, View convertView, ViewGroup parent) 
		{
			ViewHolder holder=null;
			if(convertView==null)
			{
				holder=new ViewHolder();
				convertView=inflater.inflate(R.layout.spiner_item_layout, null);
				holder.tvName=(TextView) convertView.findViewById(R.id.tv_name);
				convertView.setTag(holder);
			}
			else
			{
				holder=(ViewHolder) convertView.getTag();
			}
			holder.tvName.setText(getItem(position).toString());
			return convertView;
		}
	}
	
	private class ViewHolder
	{
		private TextView tvName;
	}
}

shape_list.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- This is the main color -->
    <item>
        <shape>
            <!-- 边框颜色 -->
            <solid android:color="#71C297"/>
        </shape>
    </item>
    <!-- 给View的上 左  右设置8dp的边框 -->
    <item
        android:left="1dp"
        android:right="1dp"
        android:bottom="1dp">
        <shape>
            <!-- View填充颜色 -->
            <solid android:color="#FFFFFFFF"/>
        </shape>
    </item>

</layer-list>

shape_tv.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <!-- 填充的颜色 -->
    <solid android:color="#FFFFFFFF" />

    <corners
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp" />
    <!-- 设置边框 -->
    <stroke
        android:width="1dp"
        android:color="#71C297" />

</shape>

spiner_window_layout.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:layout_alignParentRight="true"
    android:orientation="vertical">

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:background="@drawable/shape_list"
        android:scrollbars="none" >
    </ListView>
</LinearLayout>

spiner_item_layout.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:gravity="center_vertical">

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textColor="#666666"
        android:textSize="20sp"/>

</LinearLayout>

actity_main.xml

<TextView
        android:id="@+id/tv_value"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/shape_tv"//可以设置背景图片
        android:drawableRight="@drawable/icon_down"
        android:padding="10dp"
        android:text="请选择父类别"
        android:textColor="#666666"
        android:textSize="20sp" />


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值