您的位置:首页 > 其它

利用activity自定义dialog 开发总结

2015-06-09 09:41 302 查看
第一步:styles.xml 定义prog_dialog:

<style name="prog_dialog" parent="@android:style/Theme.Holo.Light.Dialog.NoActionBar.MinWidth"> <!--继承父theme下的一种dialog样式-->

<item name="android:windowBackground">@android:color/transparent</item> <!-- 因为要为dialog设置圆角,这里设为透明,不再有四个小黑角。dialog背景通过activity的xml文件设置 -->

<item name="android:backgroundDimEnabled">false</item><!-- 因为要通过dialog设置屏幕亮度,这里设为不模糊 -->

不要在styles.xml设置dialog的background

</style>

第二步:manifest.xml 中为activity 设置自定义style样式:

<activity

android:name=".MainTabActivity"

android:configChanges="orientation|keyboardHidden|screenSize"

android:screenOrientation="portrait"

android:theme="@style/prog_dialog">

</activity>

第三步:在drawable 中建立dialog的background ,在activity布局文件中引用:

progress_dialog_bg.xml

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

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item>

<shape>

<solid android:color="#f1111111" />

<corners android:topLeftRadius="10dp" android:topRightRadius="10dp"

android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp" />

<stroke android:width="1dp" android:color="#aaaaaa" />

</shape>

</item>

</layer-list>

activty布局文件引用backgroud: 多次试验在style.xml 的item中定义dialog的background会给tabhost 的各个组件分别加上背景,会重叠,导致背景色彩差异,特别是圆角混乱

<TabHost

android:padding="2dp"

android:background="@drawable/progress_dialog_bg"

android:id="@android:id/tabhost"

android:layout_width="match_parent"

android:layout_height="match_parent" >

<TabWidget

android:id="@android:id/tabs"

android:layout_width="match_parent"

android:layout_height="60dp"

android:layout_gravity="top">

</TabWidget>

<FrameLayout

android:id="@android:id/tabcontent"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_marginTop="60dp"

>

</FrameLayout>

</TabHost>

第四步:Java代码定义dialog显示位置

// 设置dialog位置

Window dialog = getWindow();

dialog.setGravity(Gravity.BOTTOM);

// 以下6行代码设置dialog 的宽高

WindowManager m = getWindowManager();

Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高

android.view.WindowManager.LayoutParams p = getWindow().getAttributes();

// p.height = (int) (d.getHeight() * 0.5); // 高度设置为屏幕的0.5

p.width = (int) (d.getWidth() * 0.98); // 宽度设置为屏幕的0.98

p.y =100;//设置dialog 上下偏移量,这里设为100,开发完成要测试不同分辨率手机,再调整

getWindow().setAttributes(p);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: