有时候因为项目的界面风格 很多控件如果使用原生的样式感觉会与整体风格有些不搭 所以需要自定义样式 很多项目中自定义控件都是必不可少的 这里参考网上的一些资料 实现了一个自定义Spinner 记录下 加深印象
效果图:
弹框是通过一个重写的Dialog和Listview实现
第一步 重写Dialog类SelectDialog.java
- import android.app.AlertDialog;
- import android.content.Context;
- import android.os.Bundle;
-
- public class SelectDialog extends AlertDialog {
-
- public SelectDialog(Context context, int theme) {
- super(context, theme);
- }
-
- public SelectDialog(Context context) {
- super(context);
- }
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- }
- }
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
public class SelectDialog extends AlertDialog {
public SelectDialog(Context context, int theme) {
super(context, theme);
}
public SelectDialog(Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.slt_cnt_type);
}
}
第二步重写Spinner类CustomerSpinner.java
- import java.util.ArrayList;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ListView;
- import android.widget.Spinner;
- import android.widget.AdapterView.OnItemClickListener;
-
- public class CustomerSpinner extends Spinner implements OnItemClickListener {
-
- public static SelectDialog dialog = null;
- private ArrayList<String> list;
- public static String text;
-
- public CustomerSpinner(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
-
- @Override
- public boolean performClick() {
- Context context = getContext();
- final LayoutInflater inflater = LayoutInflater.from(getContext());
- final View view = inflater.inflate(R.layout.formcustomspinner, null);
- final ListView listview = (ListView) view
- .findViewById(R.id.formcustomspinner_list);
- ListviewAdapter adapters = new ListviewAdapter(context, getList());
- listview.setAdapter(adapters);
- listview.setOnItemClickListener(this);
- dialog = new SelectDialog(context, R.style.dialog);
- LayoutParams params = new LayoutParams(650, LayoutParams.FILL_PARENT);
- dialog.setCanceledOnTouchOutside(true);
- dialog.show();
- dialog.addContentView(view, params);
- return true;
- }
-
- @Override
- public void onItemClick(AdapterView<?> view, View itemView, int position,
- long id) {
- setSelection(position);
- setText(list.get(position));
- if (dialog != null) {
- dialog.dismiss();
- dialog = null;
- }
- }
-
- public ArrayList<String> getList() {
- return list;
- }
-
- public void setList(ArrayList<String> list) {
- this.list = list;
- }
-
- public String getText() {
- return text;
- }
-
- public void setText(String text) {
- this.text = text;
- }
-
- }
import java.util.ArrayList;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemClickListener;
public class CustomerSpinner extends Spinner implements OnItemClickListener {
public static SelectDialog dialog = null;
private ArrayList<String> list;
public static String text;
public CustomerSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}
//如果视图定义了OnClickListener监听器,调用此方法来执行
@Override
public boolean performClick() {
Context context = getContext();
final LayoutInflater inflater = LayoutInflater.from(getContext());
final View view = inflater.inflate(R.layout.formcustomspinner, null);
final ListView listview = (ListView) view
.findViewById(R.id.formcustomspinner_list);
ListviewAdapter adapters = new ListviewAdapter(context, getList());
listview.setAdapter(adapters);
listview.setOnItemClickListener(this);
dialog = new SelectDialog(context, R.style.dialog);//创建Dialog并设置样式主题
LayoutParams params = new LayoutParams(650, LayoutParams.FILL_PARENT);
dialog.setCanceledOnTouchOutside(true);// 设置点击Dialog外部任意区域关闭Dialog
dialog.show();
dialog.addContentView(view, params);
return true;
}
@Override
public void onItemClick(AdapterView<?> view, View itemView, int position,
long id) {
setSelection(position);
setText(list.get(position));
if (dialog != null) {
dialog.dismiss();
dialog = null;
}
}
public ArrayList<String> getList() {
return list;
}
public void setList(ArrayList<String> list) {
this.list = list;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
这里用Listview来显示数据 ArrayList<String> list存储所要显示的数据 text存储每次spinner选中的值 在监听spinner并获取当前选中的值的时候用到
dialog的样式设置:styles.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <style name="dialog" parent="@android:style/Theme.Dialog">
- <item name="android:windowFrame">@null</item>
- <item name="android:windowIsFloating">true</item>
- <item name="android:windowIsTranslucent">false</item>
- <item name="android:windowNoTitle">true</item>
-
- <item name="android:backgroundDimEnabled">false</item>
- </style>
- </resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item><!--边框-->
<item name="android:windowIsFloating">true</item><!--是否浮现在activity之上-->
<item name="android:windowIsTranslucent">false</item><!--半透明-->
<item name="android:windowNoTitle">true</item><!--无标题-->
<!-- <item name="android:windowBackground">@color/alpha_bg</item>背景透明-->
<item name="android:backgroundDimEnabled">false</item><!--模糊-->
</style>
</resources>
Listview的代码很简单 自己写一个adapter就好了
第三步 在xml文件中引用自定义的spinner
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal" android:layout_width="fill_parent"
- android:layout_height="fill_parent" android:background="#ffffff" android:id="@+id/layout"
- >
- <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
- android:text="草帽海贼团 :" android:textColor="#000000" android:textSize="20dp"
- android:layout_marginTop="15dp"/>
- <com.spinner.test.CustomerSpinner android:background="@drawable/bg_spinner"
- android:layout_width="150dp" android:layout_height="50dp" android:id="@+id/spinner"
- android:layout_marginTop="15dp"/>
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#ffffff" android:id="@+id/layout"
>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="草帽海贼团 :" android:textColor="#000000" android:textSize="20dp"
android:layout_marginTop="15dp"/>
<com.spinner.test.CustomerSpinner android:background="@drawable/bg_spinner"
android:layout_width="150dp" android:layout_height="50dp" android:id="@+id/spinner"
android:layout_marginTop="15dp"/>
</LinearLayout>
到这里差不多就完成了 不过这个还有个缺陷 每次使用时通过 spinner.setList(list);为其加载数据 当程序返回或者退出这个节目在进入时 listview中的数据会重复 这里只能通过这个办法来解决了
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if(keyCode == KeyEvent.KEYCODE_BACK){
- list.clear();
- }
- return super.onKeyDown(keyCode, event);
- }
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
list.clear();
}
return super.onKeyDown(keyCode, event);
}
每次返回或退出时 清空list 就可以啦
下载地址:http://download.csdn.net/download/wanglj0925/4634852