由于练手用到了一些自定义dialog的小技巧,现和大家一起分享下(基本的设置可以看我之前的博文):
1.自己添加图标和标题(在窗口最上方)
可以完全用xml布局文件实现,
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <ImageView
- android:id="@+id/iconimage"
- android:layout_width="60dp"
- android:layout_height="60dp"
- android:scaleType="centerInside"//将图标集中缩放至所需宽度和高度
- android:src="@drawable/phone_holdon"
- android:layout_alignTop="@+id/texttitle"/>
- <TextView android:text="设置方式"
- android:textSize= "25dp"
- android:id="@+id/texttitle"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_toRightOf="@+id/iconimage"//相对布局 在图标的右边
- android:layout_centerInParent="true"//相对于父控件(图标)呈垂直居中
- android:layout_marginLeft="16dp">
- </TextView>
- </RelativeLayout>
最简单的就是在布局文件中添加,将已经做好的分割线放在drawable中
- <ImageView android:layout_width="fill_parent"
- android:src="@drawable/fenge5" android:layout_height="wrap_content"/>
3.我自己定义了图标和标题,如何去掉dialog本身空出的标题行(title bar)?
找了很多方法,最好的方法是用主题去描述对话框,就OK啦,在value中添加一个style.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <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">@null</item>
- <item name="android:backgroundDimEnabled">true</item><!-- 屏幕不会变暗 -->
- </style>
- </resources>
在.java中这样使用主题:
- dialogholdon dg = new dialogholdon(Preference_activity_of_huitestActivity.this,R.style.dialog);
- dg.dialogshow();
- public dialogholdon(Context context,int theme) {//构造函数
- super(context,theme);
- // TODO Auto-generated constructor stub
- }