自定义布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:layout_width="fill_parent"
>
<RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="@drawable/popup_title" android:id="@+id/about_us_rel01">
<TextView android:id="@+id/about_us_title" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关于我们" android:layout_marginLeft="20dip" android:textSize="20dip"/>
</RelativeLayout>
<RelativeLayout android:layout_height="wrap_content" android:layout_below="@+id/about_us_rel01"
android:background="@drawable/popup_bg" android:id="@+id/about_us_rel02"
android:layout_width="fill_parent" android:gravity="center" >
<ListView android:id="@+id/test_alertdialog_listview"
android:layout_height="wrap_content" android:layout_width="wrap_content"
></ListView>
</RelativeLayout>
</RelativeLayout>
java代码
final String[] datas ={"data1","data2"};
LayoutInflater inflater = this.getLayoutInflater();
View view = inflater.inflate(R.layout.test_alertdialog_listview,null);
ListView lv = (ListView)view.findViewById(R.id.test_alertdialog_listview);
lv.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,datas));
lv.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
alertdialog.dismiss();
Toast.makeText(TestAndroid.this, datas[position], Toast.LENGTH_LONG).show();
}
});
alertdialog = new AlertDialog.Builder(this)
// .setView(view);
.setItems(datas, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.create();
// 在此使用setview方法可以设置布局文件和alertdialog四周边框的距离,可以消除黑边框
alertdialog.setView(view, 0, 0, 0, 0);
alertdialog.show();
此外,我们还可以通过设置dialog的样式来定义,消除边框
使用样式文件,在values 目录下新建styles.xml文件,编写如下代码:
<resources>
<style name="dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:background">@android:color/black</item>
<item name="android:windowBackground">@null</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
调用时,使用AlerDialog的接口类,Dialog 接口编写如下代码:
Dialog dialog =
new Dialog(SetActivity.this, R.style.dialog);
dialog.setContentView(R.layout.test);
dialog.show();
下面我们查看一下Dialog的源码文件,里面的构造函数为如下:
public Dialog(Context context, int theme) {
mContext =new ContextThemeWrapper(context, theme ==0? com.android.internal.R.style.Theme_Dialog : theme);
mWindowManager = (WindowManager)context.getSystemService("window");
Window w = PolicyManager.makeNewWindow(mContext);
mWindow = w;
w.setCallback(this);
w.setWindowManager(mWindowManager, null, null);
w.setGravity(Gravity.CENTER);
mUiThread = Thread.currentThread();
mDismissCancelHandler =
new DismissCancelHandler(this);
}
这里面我们可以看出,Android 使用了默认的构造函数为Dialog 设置样式,如果没有为其设置样式,即默认加载事先编写好的样式文件,Dialog 一共由多个9.png的图片构成,大部分都是带有边框的9.png图片,所以就是为什么我们上边的样式文件要将其背景去除掉。这个东西搞了我好久,希望对你有帮助
来自:http://www.eoeandroid.com/forum-viewthread-tid-32577-highlight-%2Bdialog.html
http://www.eoeandroid.com/thread-28762-1-1.html
分享到:
相关推荐
android自定义AlertDialog,例如支付页面,先弹出自定义AlertDialog询问是否愿意支付,确定则弹出另外一个自定义AlertDialog,选择支付方式,android自定义AlertDialog,android自定义AlertDialog,android自定义...
此作为博客(打造Android通用的自定义AlertDialog提示框)的使用示例,如有问题,请留言
自定义AlertDialog对话框用于加载等耗时任务提示,博文地址http://blog.csdn.net/u012806692,在这个地址的内容中找下,因为刚写完还是404。。。。
一个简单的自定义AlertDialog
新手在自定义AlertDialog上的疑问笔者猜测主要有两个: 1、自定义的layout如何放到AlertDialog中? 解答: 获取到layout的view之后,直接调用AlertDialog.Builder的setView方法即可。 2、如何对自定义AlertDialog中...
AlertDialog.Builder(this,R.style.dialogNoBg).create(); 这里要自定义sytle <!--dialog去阴影--> <style name=dialogNoBg> <item name=android:background>#00000000 ...
自定义AlertDialog,弧形GridView
自定义AlertDialog布局
自定义AlertDialog(仿微信)
这个是看了网上很多例子后,自己优化改进写出来的例子。用法和安卓原生的显示,可以由用户自行组合出自己想要的弹出框,不会高度定制限定死各种弹出框的UI,每个部件都是独立的,如标题,内容,左按钮,右按钮,底部...
AlertDialog的简单使用及自定义AlertDialog
最近项目里有个功能点,需要使用...我需要继承AlertDialog,设置自己的内容view,重写onKeyDown方法,设置dialog的位置等...... 写了个demo. Demo是在5.0手机上测试的, 6.0以上系统还需要添加动态运行时权限的逻辑 。
比较漂亮的自定义AlertDialog。
android 自定义AlertDialog弹出键盘可以输入的
自定义AlertDialog,效果类似微信朋友圈发布弹出选择框!
本文实例讲述了Android编程自定义AlertDialog样式的方法。分享给大家供大家参考,具体如下: 开发的时候,通常我们要自定义AlertDialog来满足我们的功能需求: 比如弹出对话框中可以输入信息,或者要展示且有选择...
自定义AlertDialog_android_v1.0.0的更新 新增 可通过传入自定义的view资源自定义样式
自定义AlertDialog控件,可以自定义提示框的样式、提示框页面、标题、内容、设置按钮监听、设置1到3数量的按钮。 包含demo工程和使用说明的doc
自定义AlertDialog,颜色什么的自己都可以随意修改,,简单调用即可