第一:首先是自定义的样式:
现在一个,完整的dialog就完成了
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout<pre name="code" class="html">
android:layout_width="match_parent" android:layout_height="30dp" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提示" /> </LinearLayout> <View android:id="@+id/viewxian" android:layout_width="match_parent" android:layout_height="1dp" android:background="#000000" /> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:gravity="center_vertical" android:orientation="vertical" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确定退出百度地图?" /> </LinearLayout> <View android:id="@+id/viewxian2" android:layout_width="match_parent" android:layout_height="1dp" android:background="#89D549" /> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" > <TextView android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:layout_weight="1" android:textColor="#89D549" android:text="取消" /> <View android:id="@+id/aa" android:layout_width="1dp" android:layout_height="match_parent" android:background="#89D549" /> <TextView android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="确定" /> </LinearLayout></LinearLayout>
</pre><pre name="code" class="html">
<span style="font-size:24px;color:#ff0000;"><strong>第二:然后写一个自定义的MyDialog的类,继承系统的dialog:</strong></span>
</pre><pre name="code" class="html"><pre name="code" class="html">package com.example.didingyidialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
public class Mydialog extends Dialog{
public Mydialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public Mydialog(Context context, boolean cancelable,
OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
// TODO Auto-generated constructor stub
}
public Mydialog(Context context, int theme) {
super(context, theme);
// TODO Auto-generated constructor stub
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mydialoga);
}
}
<span style="font-size:24px;color:#ff0000;"><strong>第三:调用,在什么地方调用。一个活动中的例子</strong></span>
<span style="font-size:24px;color:#ff0000;"><strong>
</strong></span>
</pre><pre name="code" class="html">package com.example.didingyidialog;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button but=(Button)findViewById(R.id.button1xianshi);
but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Mydialog dialog=new Mydialog(MainActivity.this,R.style.MyDialog);
dialog.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
附加:当然里边有个一个Style,样式
<style name="MyDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<!-- 边框 -->
<item name="android:windowIsFloating">true</item>
<!-- 是否浮现在activity之上 -->
<item name="android:windowIsTranslucent">false</item>
<!-- 半透明 -->
<item name="android:windowNoTitle">true</item>
<!-- 无标题 -->
<!-- <item name="android:windowBackground">@drawable/ic_launcher</item> -->
<!-- 背景透明 -->
<item name="android:backgroundDimEnabled">false</item>
<!-- 模糊 -->
</style>
现在一个,完整的dialog就完成了