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

基本步骤:

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

1,从系统主题构造

     TextView sectionText ;                //显示当前字母提示的dialog用的view
Dialog sectionDlg; //显示当前字母提示的dialog void initSectionText(){
sectionText = new TextView(getActivity());
int textSize = (int) sectionText.getTextSize();
sectionText.setWidth(textSize * );
sectionText.setHeight(textSize * );
sectionText.setGravity(Gravity.CENTER);
sectionText.setTextSize(textSize * );
sectionText.setBackgroundColor(Color.parseColor("#EEE685"));
} void initDlgFromSytemTheme(){
sectionDlg = new Dialog(getActivity(), android.R.style.Theme_Dialog); sectionDlg.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
sectionDlg.setContentView(sectionText);
}

设置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,自定义的主题

     Dialog sectionDlg;                    //显示当前字母提示的dialog
TextView sectionViews[]; //各个section字母对应的TextTiew void initSectionText(){
sectionText = new TextView(getActivity());
int textSize = (int) sectionText.getTextSize();
sectionText.setWidth(textSize * );
sectionText.setHeight(textSize * );
sectionText.setGravity(Gravity.CENTER);
sectionText.setTextSize(textSize * );
sectionText.setBackgroundColor(Color.parseColor("#EEE685"));
} void initDlgFromCustomTheme(){
sectionDlg = new Dialog(getActivity(), R.style.customDlgTheme);
sectionDlg.setContentView(sectionText);
}

dialog的参数设置都在主题中

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