我实现了一个可自定义大小和位置的Dialog:CustDialog,其父类为AlertDialog

使用示例:

TestDialog t = newTestDialog(AndroidTestActivity.this,R.style.dialog); //TestDialog是CustDialog的子类

t.setLayoutResID(R.layout.chat_dialog); //设置资源布局,,在show()方法前调用

t.setPosition(x/2, y); // 设置dialog位置,在show()方法前调用

t.setRegion(150, 200); // 设置dialog大小,在show()方法前调用

t.show(); // 显示dialog

一些说明:

1, Dialog布局文件必须使用LinearLayout,其id必须是layout。

例如:res/layout/chat_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="50dp"

android:layout_height="wrap_content"android:id="@id/layout"

android:background="@color/red">

<Button android:id="@+id/btn" android:text="Button"

android:layout_width="match_parent" android:layout_height="wrap_content" />

<TextView android:id="@+id/tv" android:text="initial"

android:layout_width="match_parent" android:layout_height="wrap_content" />

</LinearLayout>

原因:layout是CustDialog实现自定义大小所必须的,使用处在CustDialog.onCreate里,如下:

layout = (LinearLayout)findViewById(R.id.layout);

android.view.ViewGroup.LayoutParams layoutParams = layout.getLayoutParams();

layoutParams.width = this.getWidth();

layoutParams.height = this.getHeight();

layout.setLayoutParams(layoutParams); // 设定大小

它定义在文件res/values/ids.xml里,如下

<?xml version="1.0" encoding="utf-8"?>

<resources>

<item type="id" name="layout" />

</resources>

2, Dialog的style定义在res/values/style.xml里,如下:

<?xmlversion="1.0" encoding="utf-8"?>

<resources>

<stylename="dialog" parent="@android:style/Theme.Dialog">

<item name="android:windowFrame">@null</item> <!--边框 -->

<item name="android:windowIsFloating">true</item> <!--是否浮现在activity之上 -->

<itemname="android:windowIsTranslucent">false</item><!--半透明 -->

<item name="android:windowNoTitle">true</item><!--无标题 -->

<itemname="android:windowBackground">@color/transparent</item><!--背景透明 -->

<itemname="android:backgroundDimEnabled">false</item><!--模糊 -->

</style>

</resources>

其中属性的中文注释或许不准确,另外还有很多其他属性可以设置,

详细可参考 android源码包中的framwworks/base/policy/src/android/internal/policy/imple/PhoneWindow.java里的函数generateLayout的代码实现,这个是无二义性的!

例子附件:http://download.csdn.net/detail/imyfriend/4083958

更多相关文章

  1. android浏览器研究-APP层UI布局
  2. Android(安卓)ListView复用机制详解
  3. android与C# WebService基于ksoap通信(Android篇)
  4. Android实现信号强度监听的方法
  5. Android(安卓)学习记录-布局预览错误
  6. 禁止应用获取手机信息
  7. android中返回页面并刷新
  8. 源码分析android 系统framework(一)之Activity 与 Window 与 View
  9. Android(安卓)TabLayout 不显示标题的解决方法

随机推荐

  1. MySQL搜索优化(用子查询替换长正则表达式)
  2. 阿里云服务器MySQL远程连接问题
  3. zabbix监控mysql、apache、交换机
  4. MySQL大战SQLite和PostgreSQL(比拼、比较
  5. MySQL中建立索引的集中方式
  6. 关于MySQL的各种总结
  7. mysql 调优 来自5.6版本官方手册
  8. MySQL中的排序(ORDER BY)
  9. 根据cookie数据连接两个表
  10. sqlsever 转mysql 出错 MySQL max_allowe