标签:android style blog http color io os ar 文件
import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.os.Bundle; import android.view.Display; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onClick(View v) { View view = View.inflate(this, R.layout.layout_listview, null); ListView lv = (ListView) view.findViewById(R.id.lv); lv.setAdapter(new MyAdapter()); AlertDialog.Builder builder = new Builder(this); final AlertDialog dialog = builder.setIcon(R.drawable.ic_launcher).setTitle("测试") .setView(view).setPositiveButton("关闭", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub } }).show(); WindowManager manager = getWindowManager(); Display display = manager.getDefaultDisplay(); int width = display.getWidth(); int height = display.getHeight(); dialog.getWindow().setGravity(Gravity.BOTTOM |Gravity.LEFT); dialog.getWindow().setLayout(width/2, height/4); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { dialog.dismiss(); Toast.makeText(MainActivity.this, "对话框消失了", Toast.LENGTH_SHORT).show(); } }); } private class MyAdapter extends BaseAdapter { @Override public int getCount() { return 5; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { return View.inflate(MainActivity.this, R.layout.layout_item, null); } } }
布局文件activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="弹出对话框" /> </RelativeLayout>
ListView布局layout_listview.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>
listview的item布局layout_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="0dip" android:layout_height="0dip" android:orientation="horizontal" > <TextView android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="AAA" /> <TextView android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="BBB" /> <TextView android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="CCC" /> <TextView android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="DDD" /> </LinearLayout>
个人认为,这个例子适合初学者,没有什么技术含量
原文:http://blog.csdn.net/ydqwym/article/details/9471993
android自定义dialog中点击listview的item事件关闭dialog
标签:android style blog http color io os ar 文件
原文地址:http://www.cnblogs.com/butterfly-clover/p/4000612.html