1.MainActivity.java
package com.future.test;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 初始化一个自定义的Dialog
Dialog dialog = new MyDialog(MainActivity.this,R.style.MyDialog);
dialog.show();
}
});
}
}
2. values下的style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyDialog" parent="@android:Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@drawable/col_opacity_0</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
</resources>
3.Mydialog类继承dialog
package com.future.test;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MyDialog extends Dialog implements OnClickListener{
private Context context;
public MyDialog(Context context, int theme) {
super(context, theme);
this.context = context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.dialog);
Button mButtonOk = (Button) this.findViewById(R.id.dialog_button_ok);
mButtonOk.setOnClickListener(this);
Button mButtonCancel = (Button) this.findViewById(R.id.dialog_button_cancel);
mButtonCancel.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.dialog_button_ok:
this.showToast("确定");
break;
case R.id.dialog_button_cancel:
this.showToast("取消");
this.dismiss();
break;
}
}
private void showToast(String text)
{
Toast.makeText(this.context, text, Toast.LENGTH_LONG).show();
}
}
4. Mydialog里面的dialog.xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:background="#18517B" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20sp"
android:textColor="#ffffff"
android:text="确认退出" />
</RelativeLayout>
<!-- 间隔线
<TextView android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@drawable/lins"/>
-->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:background="#ffffff">
<TextView
android:id="@+id/quit_true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="确定要退出游戏吗?"
android:textSize="20sp"
android:textColor="#000000"
android:gravity="center" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_below="@id/quit_true"
android:paddingTop="20dp"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:paddingBottom="20dp">
<Button
android:id="@+id/dialog_button_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/selor"
android:layout_alignParentLeft="true"
android:text="确定"/>
<Button
android:id="@+id/dialog_button_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/selor"
android:text="取消"/>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="显示自定义Dialog"
android:id="@+id/button1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</LinearLayout>
6. 在处理按钮的颜色按下变色的时候,我定义一个selor.xml文件来变换它,但是不起作用,我有在selor.xml里面又用到了一个btn_style.xml文件才能起到作用,不知道怎么回事要套两层
selor.xml文件
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/btn_style"/>
<item android:drawable="@drawable/btn_style"/>
</selector>
btn_style.xml文件
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/btn_bg_pressed"/>
<item android:drawable="@drawable/btn_bg"/>
</selector>