package qianlong.qlmobile.view; import qianlong.qlmobile.tablet.R; import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; public class MarketDialog extends Dialog { private Context context; private GridView gridview; private SelectedDialogAdapter adapter; private String[] texts; private int position = 0; OnListItemClickListener onListItemClickListener; public MarketDialog(Context context,String[] texts,int position) { super(context); // TODO Auto-generated constructor stub this.context=context; setCanceledOnTouchOutside(true); this.texts=texts; this.position=position; } public MarketDialog(Context context, int theme,String[] texts,int position) { super(context, theme); this.context = context; setCanceledOnTouchOutside(true); this.texts=texts; this.position=position; // //设置对话框位置 // Window w=getWindow(); //// w.setBackgroundDrawableResource(R.drawable.dialog_bg_1); // WindowManager.LayoutParams wl = w.getAttributes(); // int xNewPos=-130; // int yNewPos=-40; // Log.i("MarketDialog", "wl.width==="+wl.width); // Log.i("MarketDialog", "wl.height==="+wl.height); // wl.x = xNewPos; // wl.y = yNewPos; // w.setAttributes(wl); } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.market_dialog); gridview=(GridView)findViewById(R.id.gridview_dialog); gridview.setColumnWidth(1); adapter=new SelectedDialogAdapter(context,texts,position); gridview.setAdapter(adapter); gridview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub adapter.setSelectedItemPosition(position); adapter.notifyDataSetChanged(); dismiss(); onListItemClickListener.onItemClick(position); } }); } public void setOnListItemClickListener( OnListItemClickListener onListItemClickListener) { this.onListItemClickListener = onListItemClickListener; } class SelectedDialogAdapter extends BaseAdapter{ private Context context; private String[] texts; private int selectedItemPosition; public int getSelectedItemPosition() { return selectedItemPosition; } public void setSelectedItemPosition(int selectedItemPosition) { this.selectedItemPosition = selectedItemPosition; } SelectedDialogAdapter(Context context,String[] texts,int selectedItemPosition){ this.context=context; this.texts=texts; this.selectedItemPosition=selectedItemPosition; } @Override public int getCount() { // TODO Auto-generated method stub return texts.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return texts[position]; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub if(convertView==null){ LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView=inflater.inflate(R.layout.row_simple_list_item_1_for_dialog, null); } TextView tv=(TextView)convertView.findViewById(R.id.simple_item_0); tv.setText(texts[position]); if(selectedItemPosition==position){ tv.setBackgroundResource(R.drawable.btn_bg_1); }else{ tv.setBackgroundResource(R.drawable.btn_bg_2); } return convertView; } } /** * 定义接口 * @author admin * */ public interface OnListItemClickListener{ void onItemClick(int position); } }
使用到的market_dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="200dip" android:layout_height="fill_parent" android:background="@drawable/market_dialog_bg" > <TextView android:id="@+id/tv_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="选择市场" android:textColor="@color/white" android:textSize="@dimen/font_big" android:gravity="center" /> <GridView android:id="@+id/gridview_dialog" android:layout_width="fill_parent" android:layout_height="fill_parent" android:numColumns="1" android:horizontalSpacing="10dip" android:verticalSpacing="10dip" android:listSelector="@null" android:background="@drawable/news_bg" android:cacheColorHint="@null" android:padding="10dip" android:layout_margin="10dip" /> </LinearLayout>
这里使用了GridView替代ListView,反正差不多。
GridView的圆角背景news_bg.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <gradient android:startColor="@color/news_bg" android:endColor="@color/news_bg" android:angle="270" /> <stroke android:width="1px" android:color="@android:color/white" /> <!-- <solid android:color="@android:color/news_bg" /> --> <corners android:bottomRightRadius="10px" android:bottomLeftRadius="10px" android:topLeftRadius="10px" android:topRightRadius="10px" /> </shape>
使用到的图片market_dialog_bg.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <gradient android:startColor="@color/blue_light_3" android:endColor="@color/blue_light_3" android:angle="270" /> <stroke android:width="1px" android:color="@android:color/white" /> <!-- <solid android:color="@android:color/news_bg" /> --> <corners android:bottomRightRadius="20px" android:bottomLeftRadius="20px" android:topLeftRadius="20px" android:topRightRadius="20px" /> </shape>
GridView中使用的adapter文件:row_simple_list_item_1_for_dialog.xml
<?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" > <TextView android:id="@+id/simple_item_0" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:layout_gravity="center" android:textColor="@color/black" android:textSize="@dimen/font_big" /> </LinearLayout>
用法:
MarketDialog dialog=new MarketDialog(context,R.style.Theme_NoTitleDialog,markets,mMarketIndex); dialog.setOnListItemClickListener(new OnListItemClickListener() { @Override public void onItemClick(int position) { // TODO Auto-generated method stub if(mMarketIndex != position) { mMarketIndex = position; OnChangeMarketRequest(position); } } }); dialog.show();
样式:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="Theme_TransparentDialog"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowFrame">@null</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowNoTitle">true</item> <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> <item name="android:backgroundDimEnabled">false</item> </style> <style name="Theme_NoTitleDialog" parent="android:Theme.Dialog"> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@drawable/dialog_bg_1</item> <item name="android:windowContentOverlay">@null</item> <item name="android:backgroundDimEnabled">false</item> <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> </style> </resources>
效果:
android ProgressDialog自定义方法
http://www.hlovey.cn/2009/10/26/android-progressdialog-method.html
选择对话框:自定义组合控件+自定义对话框 实现
http://blog.csdn.net/billpig/article/details/6664053
Android更改自定义Dialog的背景色
http://bigcat.easymorse.com/?p=1143
OPhone 2.0之百变对话框
http://www.ophonesdn.com/article/show/279