Android Dialogs(6)Dialog类使用示例:用系统theme和用自定义的theme

使用dialog时有很多 方法,其中一个就是直接 使用基类Dialog,可用来作一个没有按钮的非模态提示框,它可以直接从系统的主题构造也可从自定义的主题构造。

基本步骤:

  • a,构造
  • b,调用dialot.show()
  • c,设置显示参数,注意用代码设置dialog显示参数要在dialog初始化之后,否则无效,在show之后就可以,如:dimAmout,alpha,width,height等 

1,从系统主题构造

 1     TextView sectionText ;                //显示当前字母提示的dialog用的view
 2     Dialog sectionDlg;                    //显示当前字母提示的dialog
 3     
 4     void initSectionText(){
 5         sectionText = new TextView(getActivity());
 6         int textSize = (int) sectionText.getTextSize();
 7         sectionText.setWidth(textSize * 6);
 8         sectionText.setHeight(textSize * 6);
 9         sectionText.setGravity(Gravity.CENTER);
10         sectionText.setTextSize(textSize * 2);
11         sectionText.setBackgroundColor(Color.parseColor("#EEE685"));
12     }
13     
14     void initDlgFromSytemTheme(){
15         sectionDlg =  new Dialog(getActivity(), android.R.style.Theme_Dialog);
16 
17                 sectionDlg.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
18                 sectionDlg.setContentView(sectionText);
19     }

设置dialog的显示参数

     sectionDlg.show();
     Window dialogWindow = sectionDlg.getWindow(); WindowManager.LayoutParams wlp = dialogWindow.getAttributes(); dialogWindow.setGravity(Gravity.CENTER); /*wlp.width 可指定具体值,也可指定为ViewGroup.LayoutParams.WRAP_CONTENT等 */ wlp.width = ViewGroup.LayoutParams.WRAP_CONTENT; wlp.height = ViewGroup.LayoutParams.WRAP_CONTENT; wlp.alpha = 0.5f;//dialog显示区域透明度。 wlp.dimAmount = 0.0f;//设置dialog背景灰度

 

2,自定义的主题

 1     Dialog sectionDlg;                    //显示当前字母提示的dialog
 2     TextView sectionViews[];            //各个section字母对应的TextTiew
 3     
 4     void initSectionText(){
 5         sectionText = new TextView(getActivity());
 6         int textSize = (int) sectionText.getTextSize();
 7         sectionText.setWidth(textSize * 6);
 8         sectionText.setHeight(textSize * 6);
 9         sectionText.setGravity(Gravity.CENTER);
10         sectionText.setTextSize(textSize * 2);
11         sectionText.setBackgroundColor(Color.parseColor("#EEE685"));
12     }
13     
14     void initDlgFromCustomTheme(){
15         sectionDlg =  new Dialog(getActivity(), R.style.customDlgTheme);
16                 sectionDlg.setContentView(sectionText);
17     }

dialog的参数设置都在主题中

1   <style name="customDlgTheme"  parent="@android:style/Theme.Dialog">  
2         <item name="android:windowIsFloating">true</item><!-- 是否浮现在activity之上 -->
3         <item name="android:windowFrame">@null</item><!-- 边框 -->
4         <item name="android:windowNoTitle">true</item> <!-- 无标题栏 -->
5         <item name="android:windowIsTranslucent">false</item><!-- 是否半透明,背景图或背景色的透明度优先于此 -->
6         <item name="android:windowBackground">@drawable/selector_dialog_bg</item><!-- 背景透明 -->
7         <item name="android:backgroundDimEnabled">false</item><!--是否开启dlg显示时后面的灰暗背景,false表示不开启 -->
8         <item name="android:backgroundDimAmount">1.0</item> <!-- dlg显示时灰暗背景的灰暗值,0.0是无灰暗背景,它的优先级低于backgroundDimEnabled -->
9     </style>

 

转载于:https://www.cnblogs.com/sjjg/p/5029378.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值