先看图:
布局search_dialog.xml:
应用之:
试用之:
SearchDialog dialog=new SearchDialog(Test_3_Activity.this,R.style.Theme_NoTitleDialog);
dialog.show();
上面的代码试用的是带有Theme的第2个构造函数,这样可以指定Dialog的样式
若使用第一个构造函数,则需要带有title:
SearchDialog dialog=new SearchDialog(Test_3_Activity.this);
dialog.setTitle("Search Item");
dialog.show();
效果如下:
http://gundumw100.iteye.com/admin/blogs/869742
任意地方显示对话框
改变Android 对话框位置及边框
关键是取得Window
Window w=getWindow();
修改边框:
w.setBackgroundDrawableResource(rc);
rc为资源ID
改变位置:
WindowManager.LayoutParams wl = w.getAttributes();
wl.x = xNewPos;
wl.y = yNewPos;
w.setAttributes(wl);
1、对话框缺省居中wl.x=0,wl.y=0
新坐标 x小于0左移,大于0右移;y小于0上移,大于0下移
2、无论x,y设什么值,对话框也不会移出到屏幕外。
我试过x,y设成-1000,显示在左上角,没移出去。
自定义对话框的大小
Android dialog 全屏
其中transparent_background为颜色值:#50000000,透明度为50
然后:
final Dialog dialog = new Dialog(this,R.style.Transparent);

布局search_dialog.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageButton android:id="@+id/btn_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:src="@drawable/search" /> <EditText android:id="@+id/et_search" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_toLeftOf="@id/btn_search" /> <ListView android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/et_search" android:listSelector="@drawable/button_blue" /> </RelativeLayout>
应用之:
import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; import com.ql.activity.R; public class SearchDialog extends Dialog{ private Context context; private EditText et_search; private ImageButton btn_search; private ListView listview; public SearchDialog(Context context) { super(context); // TODO Auto-generated constructor stub this.context = context; } public SearchDialog(Context context, int theme) { super(context, theme); this.context = context; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.search_dialog); et_search = (EditText)findViewById(R.id.et_search); btn_search = (ImageButton)findViewById(R.id.btn_search); listview=(ListView)findViewById(R.id.listview); ArrayList<Map<String,String>> data=new ArrayList<Map<String,String>>(); Map<String,String> map=null; for(int i=0;i<10;i++){ map=new HashMap<String,String>(); map.put("simple_item_1", "item"+i); data.add(map); } int resource=R.layout.row_simple_list_item_1; String[] from={"simple_item_1"}; int[] to={R.id.simple_item_1}; SimpleAdapter adapter=new SimpleAdapter(context, data, resource, from, to); listview.setAdapter(adapter); listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { // TODO Auto-generated method stub TextView tv=(TextView)v.findViewById(R.id.simple_item_1); et_search.setText(tv.getText()); } }); btn_search.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(context, "searching", Toast.LENGTH_SHORT).show(); } }); } }
试用之:
SearchDialog dialog=new SearchDialog(Test_3_Activity.this,R.style.Theme_NoTitleDialog);
dialog.show();
上面的代码试用的是带有Theme的第2个构造函数,这样可以指定Dialog的样式
<style name="Theme_NoTitleDialog" parent="android:Theme.Dialog"> <item name="android:windowNoTitle">true</item> </style>
若使用第一个构造函数,则需要带有title:
SearchDialog dialog=new SearchDialog(Test_3_Activity.this);
dialog.setTitle("Search Item");
dialog.show();
效果如下:

http://gundumw100.iteye.com/admin/blogs/869742
任意地方显示对话框
AlertDialog.Builder builder = new AlertDialog.Builder(this); LayoutInflater inf = getLayoutInflater(); View layout = inf.inflate(R.layout.main, null); builder.setView(layout); builder.setTitle("Add to Home screen"); AlertDialog dialog = builder.create(); WindowManager.LayoutParams WMLP = dialog.getWindow().getAttributes(); int dialogOriginalHeight = WMLP.height; WMLP.height += 750; Log.i("XnY", "x="+WMLP.x+", y="+WMLP.y); WMLP.x = -10; //x position WMLP.y = -10; //y position Log.i("XnY", "x="+WMLP.x+", y="+WMLP.y); dialog.getWindow().setAttributes(WMLP); Log.i("POSITION", "POS::HEIGHT:"+WMLP.height); dialog.show();
改变Android 对话框位置及边框
关键是取得Window
Window w=getWindow();
修改边框:
w.setBackgroundDrawableResource(rc);
rc为资源ID
改变位置:
WindowManager.LayoutParams wl = w.getAttributes();
wl.x = xNewPos;
wl.y = yNewPos;
w.setAttributes(wl);
1、对话框缺省居中wl.x=0,wl.y=0
新坐标 x小于0左移,大于0右移;y小于0上移,大于0下移
2、无论x,y设什么值,对话框也不会移出到屏幕外。
我试过x,y设成-1000,显示在左上角,没移出去。
自定义对话框的大小
WindowManager m = getWindowManager(); Display d = m.getDefaultDisplay(); //为获取屏幕宽、高 LayoutParams p = getWindow().getAttributes(); //获取对话框当前的参数值 p.height = (int) (d.getHeight() * 0.6); //高度设置为屏幕的0.6 p.width = (int) (d.getWidth() * 0.95); //宽度设置为屏幕的0.95 getWindow().setAttributes(p); //设置生效
Android dialog 全屏
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="Transparent"> <item name="android:windowBackground">@color/transparent_background</item> <item name="android:windowNoTitle">true</item> <item name="android:windowFullscreen">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowAnimationStyle">@+android:style/Animation.Translucent</item> </style> </resources>
其中transparent_background为颜色值:#50000000,透明度为50
然后:
final Dialog dialog = new Dialog(this,R.style.Transparent);