自定义一个ListView,MyListView并定义构造函数,并载入一个布局文件
里面放两个TextVIew,一个ImageView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
>
<com.example.zzq.b74.MyListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:id="@+id/lsvTest"/>
</LinearLayout>
listview_select.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:paddingTop="10dp" android:paddingBottom="4dp"> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:layout_weight="1"> <TextView android:id="@+id/tvListSelectLayoutTitle" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="0dp" android:text="浏览模式"/> <TextView android:id="@+id/tvListSelectLayoutContent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="10dp" android:paddingTop="0dp" android:text="适应屏幕"/> </LinearLayout> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="fitCenter" android:background="@drawable/ic_btn_round_more_normal" android:layout_marginRight="0dp"/> </LinearLayout>
public class MyListView extends LinearLayout { private TextView tvHeader; private TextView tvContent; public MyListView(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.listview_select, this, true); tvHeader = (TextView) findViewById(R.id.tvListSelectLayoutTitle); tvContent = (TextView) findViewById(R.id.tvListSelectLayoutContent); tvHeader.setTextColor(Color.BLACK); tvContent.setGravity(Gravity.TOP); } }
下面是让点击图片时显示一个Dialog
添加点击事件
public class MyListView extends LinearLayout implements View.OnClickListener {
private TextView tvHeader;
private TextView tvContent;
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.listview_select, this, true);
tvHeader = (TextView) findViewById(R.id.tvListSelectLayoutTitle);
tvContent = (TextView) findViewById(R.id.tvListSelectLayoutContent);
tvHeader.setTextColor(Color.BLACK);
tvContent.setGravity(Gravity.TOP);
this.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Context context= this.getContext();
MyDialog dialog =new MyDialog.Builder(this.getContext()).create();
dialog.show();
}
}
新建一个自定义Dialog并添加一个静态内部类Builder,创建Builder的create方法创建Dialog
public class MyDialog extends Dialog{
Context context;
public MyDialog(Context context, int themeResId) {
super(context, themeResId);
}
public static class Builder {
Context context;
public Builder(Context context) {
this.context = context;
}
public MyDialog create() {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.choicelist, null);
final MyDialog dialog = new MyDialog(context, R.style.Theme_Dialog_ListSelect);
dialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
return dialog;
}
}
}
创建Dialog时添加View对象choicelist.xml并设置布局参数,
里面有一个TextView和一个ListView
choicelist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="浏览模式"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView
android:id="@+id/lvListItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
</LinearLayout>
这时对话框只能显示四个字,黑乎乎一片
需要往ListView里面添加选择则按钮,绑定Adapter
if(mListItem == null){
throw new RuntimeException("Entries should not be empty");
}
ListView lvListItem = (ListView) layout.findViewById(R.id.lvListItem);
lvListItem.setAdapter(new ArrayAdapter(context, R.layout.single_selection_list_item, R.id.ctvListItem, mListItem));
single_selection_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ctvListItem">
</CheckedTextView>
需要把选择按钮参数传入MyListView
在MyListView中添加三个参数
<pre style="font-family: 宋体; font-size: 9pt; background-color: rgb(255, 255, 255);"><pre name="code" class="java">private CharSequence[] mEntries;//选项名 private CharSequence[] mEntryValues;//选项值 private int mClickedDialogEntryIndex;//选中的选项序号
定义参数attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources >
<declare-styleable name="ListSelectView">
<attr name="entries" format="reference"/>
<attr name="entryValues" format="reference" />
</declare-styleable>
</resources>
定义一个字符串数组资源
array.xmlmain.xml中给entries和entryValues赋值<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="entries_list_preference"> <item>适应屏幕</item> <item>缩放模式(压缩)</item> <item>缩放模式</item> </string-array> <string-array name="entriesvalue_list_preference"> <item>1</item> <item>2</item> <item>3</item> </string-array> </resources>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/white" > <com.example.zzq.b74.MyListView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:lansir="http://schemas.android.com/apk/res/com.example.zzq.b74" lansir:entries="@array/entries_list_preference" lansir:entryValues="@array/entries_list_preference" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="10dp" android:paddingRight="10dp" android:id="@+id/lsvTest"/> </LinearLayout>
在MyListView中接收参数
在MyDialog.Build中创建方法TypedArray a = context.obtainStyledAttributes(attrs, org.lansir.R.styleable.ListSelectView, 0, 0); mEntries = a.getTextArray(org.lansir.R.styleable.ListSelectView_entries); mEntryValues = a.getTextArray(org.lansir.R.styleable.ListSelectView_entryValues); a.recycle();
接收传入的参数
public Builder setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener) {
this.mListItem = items;
this.mOnClickListener = listener;
this.mClickedDialogEntryIndex = checkedItem;
return this;
}
创建Dialog时传入参数,并设置选择后的操作,实现接口传回当前选项,并改变主界面中文字,关闭对话框
@Override
public void onClick(View v) {
Context context= this.getContext();
MyDialog dialog =new MyDialog.Builder(this.getContext())
.setSingleChoiceItems(mEntries,
mClickedDialogEntryIndex,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mClickedDialogEntryIndex = which;
tvContent.setText(mEntryValues[mClickedDialogEntryIndex]);
dialog.dismiss();
}
}
)
.create();
dialog.show();
}
在点击Listview选项后调用接口,并传回选择的序号
lvListItem.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
mOnClickListener.onClick(dialog, position);
}
});
设置为单选,并设置选中的选项
lvListItem.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lvListItem.setItemChecked(mClickedDialogEntryIndex, true);
lvListItem.setSelection(mClickedDialogEntryIndex);
TextView tvHeader = (TextView)layout.findViewById(R.id.title);
tvHeader.setText("浏览模式");
return dialog;
给选项添加样式,?是系统自带的
</pre><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?> <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ctvListItem" android:layout_width="match_parent" android:layout_height="?android:attr/listPreferredItemHeight" android:textAppearance="?android:attr/textAppearanceLarge" android:gravity="center_vertical" android:checkMark="?android:attr/listChoiceIndicatorSingle" android:paddingLeft="6dip" android:paddingRight="6dip" android:textColor="@color/black" />