简述:
需要自定义一个AlertDialog来显示一些警告框,只需要修改一下背景图片和相应在mydialog.xml中的布局信息就可以自定义了
知识点:
1. 自定义警告框
2. selector修改点击行为的图片切换
3. RelativeLayout的排版
代码:
主界面的xml界面文件
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainRelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/myBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show dialog"
android:textSize="22dip"
/>
</RelativeLayout>
主界面显示的Java代码
MainActivity.java
package com.atp.ui;
import com.atp.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button myBtn = null;
private AlertDialog myDialog = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
myBtn = (Button) findViewById(R.id.myBtn);
myBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
myDialog = new AlertDialog.Builder(MainActivity.this).create();
myDialog.show();
myDialog.getWindow().setContentView(R.layout.mydialog);
myDialog.getWindow()
.findViewById(R.id.button_back_mydialog)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myDialog.dismiss();
}
});
}
});
}
}
mydialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imageView_background_mydialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/pop_up" />
<!-- Line 1 -->
<ImageView
android:id="@+id/imageView_warning_mydialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/imageView_background_mydialog"
android:layout_alignTop="@id/imageView_background_mydialog"
android:layout_marginLeft="20dip"
android:layout_marginTop="30dip"
android:src="@drawable/success" />
<TextView
android:id="@+id/textView_title_mydialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/imageView_warning_mydialog"
android:layout_alignTop="@id/imageView_background_mydialog"
android:layout_marginLeft="20dip"
android:layout_marginTop="30dp"
android:text="success AlertDialog"
android:textColor="@color/white"
android:textSize="22dp" >
</TextView>
<!-- Line 2 -->
<TextView
android:id="@+id/textView_mydialog"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/imageView_background_mydialog"
android:layout_alignTop="@id/imageView_background_mydialog"
android:layout_marginLeft="30dip"
android:layout_marginTop="87dip"
android:text="HELLO WORLD"
android:textColor="@color/white"
android:textSize="15dp" />
<!-- Line 3 -->
<Button
android:id="@+id/button_back_mydialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView_background_mydialog"
android:layout_marginBottom="20dp"
android:layout_centerHorizontal="true"
android:background="@drawable/button_empty_selector"
android:text="CANCEL" />
</RelativeLayout>
自定义图片的动态切换
button_empty_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_press_9" android:state_pressed="true"></item>
<item android:drawable="@drawable/button_normal_9" android:state_pressed="false"></item>
</selector>
实际效果:
点击前,
点击之后,