Android开发学习
Android开发中,会需要对自己定义自己的弹出提示层,不使用系统默认的。
新建布局文件:activity_main.xml,代码内容:(显示需要点击弹出层对话框的页)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/btn_dialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn_dialog"></Button> </RelativeLayout>
新建布局文件:dialog_layout.xml,代码内容:(弹出层对话框的页)
<?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" > <!-- 顶部椭园边缘 --> <ImageView android:layout_marginBottom="-2dip" android:layout_width="307dip" android:layout_height="12dip" android:layout_marginLeft="6dip" android:src="@drawable/dialog_top" > </ImageView> <!-- 中间白色背景,两个TextView,标题和内容,留一个LinearLayout,在代码中根据调用动态加上按钮 --> <LinearLayout android:layout_width="307dip" android:layout_height="wrap_content" android:layout_marginLeft="6dip" android:background="@drawable/dialog_center" android:orientation="vertical" > <TextView android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="#000000" android:textSize="35dip" /> <TextView android:id="@+id/message" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dip" android:layout_marginLeft="20dip" android:layout_marginRight="10dip" android:layout_marginTop="20dip" android:textColor="#000000" android:textSize="25dp" /> <!-- 在LinearLayout中加按钮 --> <LinearLayout android:id="@+id/buttonLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:gravity="center" android:orientation="horizontal" > </LinearLayout> </LinearLayout> <!-- 底部椭园边缘 --> <ImageView android:layout_marginTop="-2dip" android:layout_width="307dip" android:layout_height="12dip" android:layout_marginLeft="6dip" android:src="@drawable/dialog_bottom" > </ImageView> </LinearLayout>
实现的MainActivity.java代码如下:(点击弹出对话层)
package com.zhengdecai.myalertdialog;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
/**
* 自定义弹出层(样式通过xml设置)
*
* @author 郑德才
*
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_dialog = (Button) findViewById(R.id.btn_dialog);
btn_dialog.setOnClickListener(new LinearLayout.OnClickListener() {
@Override
public void onClick(View v) {
final AlertDialog ad = new AlertDialog(MainActivity.this);
ad.setTitle("标题");
ad.setMessage("内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容");
ad.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ad.dismiss();
Toast.makeText(MainActivity.this, "被点到确定",
Toast.LENGTH_LONG).show();
}
});
ad.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ad.dismiss();
Toast.makeText(MainActivity.this, "被点到取消",
Toast.LENGTH_LONG).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;
}
}实现的AlertDialog.java代码如下:(对话框实现)
package com.zhengdecai.myalertdialog;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
/**
* 自定义AlertDialog对话框
*
* @author 郑德才
*
*/
public class AlertDialog {
Context context;
android.app.AlertDialog ad;
TextView titleView;
TextView messageView;
LinearLayout buttonLayout;
public AlertDialog(Context context) {
// TODO Auto-generated constructor stub
this.context = context;
ad = new android.app.AlertDialog.Builder(context).create();
ad.show();
// 关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
Window window = ad.getWindow();
window.setContentView(R.layout.dialog_layout);
titleView = (TextView) window.findViewById(R.id.title);
messageView = (TextView) window.findViewById(R.id.message);
buttonLayout = (LinearLayout) window.findViewById(R.id.buttonLayout);
}
public void setTitle(int resId) {
titleView.setText(resId);
}
public void setTitle(String title) {
titleView.setText(title);
}
public void setMessage(int resId) {
messageView.setText(resId);
}
public void setMessage(String message) {
messageView.setText(message);
}
/**
* 设置按钮
*
* @param text
* @param listener
*/
public void setPositiveButton(String text,
final View.OnClickListener listener) {
Button button = new Button(context);
LinearLayout.LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setBackgroundResource(R.drawable.dialog_button);
button.setText(text);
button.setTextColor(Color.WHITE);
button.setTextSize(20);
button.setOnClickListener(listener);
buttonLayout.addView(button);
}
/**
* 设置按钮
*
* @param text
* @param listener
*/
public void setNegativeButton(String text,
final View.OnClickListener listener) {
Button button = new Button(context);
LinearLayout.LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setBackgroundResource(R.drawable.dialog_button);
button.setText(text);
button.setTextColor(Color.WHITE);
button.setTextSize(20);
button.setOnClickListener(listener);
if (buttonLayout.getChildCount() > 0) {
params.setMargins(20, 0, 0, 0);
button.setLayoutParams(params);
buttonLayout.addView(button, 1);
} else {
button.setLayoutParams(params);
buttonLayout.addView(button);
}
}
/**
* 关闭对话框
*/
public void dismiss() {
ad.dismiss();
}
}实现运行效果图:


评论列表: