请教:android可否将一个包含图片、按钮、文字、编辑框的layout布局以对话框形式显示 目前项目需要在前端显示小窗口,里面包含内容比较多,有图片、按钮、文字、编辑框都可以按照一个xml格式写成layout布局了不知道自定义对话框可否显示出来如果不能,那别的思路该是什么呢?请教 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以啊!你先写个布局文件比如xx.xml;这个布局文件中包含你要显示的那些内容;然后通过一下代码来获取你的布局文件:LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);View view=inflater.inflate(R.layout.inflater, null);得到你的布局文件之后,你就可以引用你布局文件中的内容,比如:ImageButton imageButton=(ImageButton)view.findViewById(R.id.imageButton);取得这些控件对象之后,就可以设置它们的值了。设置好之后,再把view添加到对话框中。使用addView(view);方法即可!不懂的话,可以去看下这个http://blog.csdn.net/android_tutor/article/details/5513869 可以啊,比如一个例子servicelistxml<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Name:" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/editName"></EditText> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Age:"></TextView> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/editAge" android:inputType="number"></EditText> </LinearLayout> </LinearLayout> 程序中使用,mainmap为ActivityLayoutInflater layoutInflater = LayoutInflater.from(mainmap); View AddEmployee = layoutInflater.inflate(R.layout.servicelist, null); new AlertDialog.Builder(mainmap).setTitle("更改地图").setView( AddEmployee).setPositiveButton("应用", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //insertEmployee(); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).show(); http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html这里里面最后一种就是你要的形式。 谢谢各位,有思路了还有一个小问题,因为项目需要很多种对话框,我打算用onCreateDialog都创建出来怎么自定义对话框大小呢网上的代码WindowManager m = getWindowManager(); Display d = m.getDefaultDisplay(); //为获取屏幕宽、高 WindowManager.LayoutParams p = getWindow().getAttributes(); //获取对话框当前的参数值 p.x = 0; //设置位置 默认为居中p.y = 0; //设置位置 默认为居中p.height = (int) (d.getHeight() * 0.5); //高度设置为屏幕的0.6 p.width = (int) (d.getWidth() * 0.5); //宽度设置为屏幕的0.95 this.getWindow().setAttributes((android.view.WindowManager.LayoutParams) p);一下子把Activity都变小了 view.setMinimumHeight(***);v.setMinimumWidth(***);已经实现了,嘿嘿 Android里该怎么做出音乐频谱的动画? Android重力感应之谈 求救,怎查找Anadroid Api 帮忙看看http get请求有什么问题 如何在tabActivity的一个tab标签页中使用googleMap? 怎么遍历服务器下的一个文件夹 关于蓝牙权限的问题 关于Future的使用 怎样开发跨安卓和IOS两个平台的项目? 请问关于checkbox的选择问题 100分询问 AppWidget 上如果实现一张图片渐缩放的效果 怎么去掉tabhost默认的选项卡
LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.inflater, null);
得到你的布局文件之后,你就可以引用你布局文件中的内容,比如:
ImageButton imageButton=(ImageButton)view.findViewById(R.id.imageButton);
取得这些控件对象之后,就可以设置它们的值了。
设置好之后,再把view添加到对话框中。使用addView(view);方法即可!
不懂的话,可以去看下这个http://blog.csdn.net/android_tutor/article/details/5513869
servicelistxml<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="horizontal">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Name:" />
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/editName"></EditText>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Age:"></TextView>
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/editAge" android:inputType="number"></EditText>
</LinearLayout>
</LinearLayout>
程序中使用,mainmap为ActivityLayoutInflater layoutInflater = LayoutInflater.from(mainmap);
View AddEmployee = layoutInflater.inflate(R.layout.servicelist, null);
new AlertDialog.Builder(mainmap).setTitle("更改地图").setView(
AddEmployee).setPositiveButton("应用",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//insertEmployee();
}
}).setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
WindowManager m = getWindowManager();
Display d = m.getDefaultDisplay(); //为获取屏幕宽、高 WindowManager.LayoutParams p = getWindow().getAttributes();
//获取对话框当前的参数值
p.x = 0; //设置位置 默认为居中
p.y = 0; //设置位置 默认为居中
p.height = (int) (d.getHeight() * 0.5); //高度设置为屏幕的0.6
p.width = (int) (d.getWidth() * 0.5); //宽度设置为屏幕的0.95
this.getWindow().setAttributes((android.view.WindowManager.LayoutParams) p);一下子把Activity都变小了
v.setMinimumWidth(***);已经实现了,嘿嘿