使用系统自带的dialog如果不能满足我们日常开发的需求,那就得自己构建custom dialog,特别是对于一个app来说,统一的样式风格会给人一种舒服的感觉,所以dialog的样式 、色调一般都要和app主题符合,这篇博客主要介绍两种方式来自定义dialog。
MainActivity的代码
- package com.example.e01_consumerdialog;
- import android.os.Bundle;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.AlertDialog.Builder;
- import android.content.DialogInterface;
- import android.content.DialogInterface.OnClickListener;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.View;
- import android.widget.Button;
- public class MainActivity extends Activity {
- private Button button;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- button=(Button)this.findViewById(R.id.button1);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- AlertDialog.Builder builder=new Builder(MainActivity.this);
- builder.setTitle("登陆界面");
- View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog, null);
- builder.setView(view);//设置自定义布局view
- builder.setPositiveButton("确认", new OnClickListener() {
- @Override
- public void onClick(DialogInterface arg0, int arg1) {
- // TODO Auto-generated method stub
- }
- });
- builder.setNegativeButton("取消",new OnClickListener() {
- @Override
- public void onClick(DialogInterface arg0, int arg1) {
- // TODO Auto-generated method stub
- }
- });
- builder.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;
- }
- }
dialog.xml的布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@+id/editText1"
- android:layout_marginTop="27dp"
- android:layout_toLeftOf="@+id/editText1"
- android:text="密 码:" />
- <EditText
- android:id="@+id/editText2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignBaseline="@+id/textView2"
- android:layout_alignBottom="@+id/textView2"
- android:layout_alignLeft="@+id/editText1"
- android:layout_alignRight="@+id/editText1"
- android:ems="10"
- android:inputType="textPassword" />
- <EditText
- android:id="@+id/editText1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignBaseline="@+id/textView1"
- android:layout_alignBottom="@+id/textView1"
- android:layout_toRightOf="@+id/textView1"
- android:ems="10" />
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:text="用户名:" />
- </RelativeLayout>
以上的自定义dialog是模拟实现了一个登陆框选项的功能,但缺点也比较明显,按钮必须还得使用dialog本身的 PositiveButton这种风格,那有没有使我们的权限更大一点的自定义方法呢,从官方API我们可以看到
Tip: If you want a custom dialog, you can instead display an Activity
as a dialog instead of using the Dialog
APIs. Simply create an activity and set its theme to Theme.Holo.Dialog
in the <activity>
manifest element:
<activity android:theme="@android:style/Theme.Holo.Dialog" >
That's it. The activity now displays in a dialog window instead of fullscreen.
上面的tip告诉我们如果想自定义dialog,还可以把自己做的activity在清单文件中添加dialog属性,以此实现dialog形式的出现效果!