ch016 Android 自定义对话框.
--------------------------------------------AndroidManifest.xml----------------------------------
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ch16"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
--------------------------------------------Layout 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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout>
--------------------------------------------Layout mydialog.xml---------------------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/dialog_bg"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/ico"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:src="@drawable/dialog_title_image" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_toRightOf="@+id/ico"
android:text="Title"
android:textSize="42dp" />
</RelativeLayout>
<!-- 中间分割线 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="1dp"
android:background="@drawable/lins" />
<!-- 消息体 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="这是一个自定义Dialog" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="20dp" >
<Button
android:id="@+id/ok"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="确定" />
<Button
android:id="@+id/btn_canal"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/ok"
android:text="取消" />
</RelativeLayout>
</LinearLayout>
--------------------------------------------MainActivity.java--------------------------------------
package com.ch16;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* 实例化自定义对话框 */
MyDialog dialog = new MyDialog(this, R.style.mydialog);
/* 显示对话框 */
dialog.show();
}
}
--------------------------------------------MyDialog.java----------------------------------
package com.ch16;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
/**
*
* 项目名称:com.ch16
* 类名称:MyDialog
* 类描述: 自定义对话框
* 创建人:方勇
* 创建时间:2012-11-26 上午11:28:39
* Copyright (c) 方勇-版权所有
*/
public class MyDialog extends Dialog {
/* 确定按钮 */
private Button btn_ok;
/* 上下文 */
private Context mcontext;
public MyDialog(Context context) {
super(context);
mcontext = context;
}
public MyDialog(Context context, int theme) {
super(context, theme);
mcontext = context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mydialog);
findViews();
setListeners();
}
/* 实例化UI */
private void findViews() {
btn_ok = (Button) findViewById(R.id.ok);
}
/* 设置确定按钮监听器 */
private void setListeners() {
btn_ok.setOnClickListener(onClickListener);
}
private View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
showResults();
}
};
/* 显示结果 */
private void showResults() {
Toast.makeText(mcontext, "test......", Toast.LENGTH_LONG).show();
}
}
--------------------------------------------Style mydialog.xml-----------------------------------
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="mydialog" parent="android:Theme.Dialog" >
<item name="android:windowFrame">@null</item>
<!-- 无标题 -->
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@drawable/dialog_bg</item>
<!-- 悬浮效果 -->
<item name="android:windowIsFloating">true</item>
<!-- 遮罩效果 -->
<item name="android:windowContentOverlay">@null</item>
</style>
</resources>
--------------------------------------------效果图--------------------------------------------------
上一篇: Android 用户更愿意点击广告?
下一篇: ProgressDialog用法详解