最近做项目,由于要兼容2.3版本的系统,所以很多控件需要自定义,现在写一下自定义的dialog,以便各个版本的系统显示的dialog都一样,所以需要自定义样式。
先看下样式:
下面是主代码:
<span style="font-size:18px;">package com.example.MyDialog;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.*;
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//初始化MyDialog,并传入样式R.style.add_dialog参数
MyDialog myDialog = new MyDialog(MyActivity.this, R.style.add_dialog);
myDialog.setContentView(R.layout.dialog_layout);
//获取屏幕宽度参数
Display display = getWindowManager().getDefaultDisplay();
//获取dialog的窗口
Window dialogWindow = myDialog.getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//设置窗口的宽度:屏幕宽度减去40dp
lp.width = display.getWidth() - dip2px(MyActivity.this, 40);
//设置窗口高度,wrap_content
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
//设置窗口居中
dialogWindow.setGravity(Gravity.CENTER);
dialogWindow.setAttributes(lp);
//显示窗口
myDialog.show();
}
});
}
//dp转px工具
public int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
}
</span>
MyDialog类代码:
<span style="font-size:18px;">package com.example.MyDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
/**
* Created by PQ on 2014/12/26.
*
* 自定义dialog
*/
public class MyDialog extends Dialog implements View.OnClickListener {
private Button mBtnCancel;
private Button mBtnSure;
private Context mConext;
public MyDialog(Context context) {
super(context);
}
public MyDialog(Context context, int theme) {
super(context, theme);
mConext = context;
}
protected MyDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_layout);
mBtnCancel = (Button) findViewById(R.id.btn_cancel);
mBtnSure = (Button) findViewById(R.id.btn_sure);
mBtnSure.setOnClickListener(this);
mBtnCancel.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view == mBtnCancel) {
//点击取消
Toast.makeText(mConext, "取消", Toast.LENGTH_SHORT).show();
} else if (view == mBtnSure) {
//点击确定按钮,处理你需要处理的数据
Toast.makeText(mConext, "确定", Toast.LENGTH_SHORT).show();
}
dismiss();
}
}
</span>
主布局代码:很简单就一个按钮:
<span style="font-size:18px;"><?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">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, MyActivity" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击弹出dialog" />
</LinearLayout></span>
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_white">
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:textColor="@color/color_blue"
android:text="标题"
android:textSize="18sp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/color_blue" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="内容"
android:padding="10dp"
android:textColor="@color/color_blue" />
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/color_gray" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<Button
android:id="@+id/btn_cancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/button_dialog_selector"
android:text="取消" />
<ImageView
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/color_gray" />
<Button
android:id="@+id/btn_sure"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/button_dialog_selector"
android:text="确定" />
</LinearLayout>
</LinearLayout></span>
样式style代码:
下面的
<item name="android:windowBackground">@color/color_white</item>
<!-- 自己想要的背景 -->
换成
<item name="android:windowBackground">@android:color/transparent</item>
就可以设置背景是透明的,然后在xml中设置背景可以是圆角。
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- dialog -->
<style name="add_dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<!-- 边框 -->
<item name="android:windowIsFloating">true</item>
<!-- 是否浮现在activity之上 -->
<item name="android:windowIsTranslucent">false</item>
<!-- 半透明 -->
<item name="android:windowNoTitle">true</item>
<!-- 无标题 -->
<item name="android:windowBackground">@color/color_white</item>
<!-- 自己想要的背景 -->
<item name="android:backgroundDimEnabled">true</item>
<!-- 模糊 -->
</style>
</resources></span>
按钮selector代码:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_dialog_shape_pressed" android:state_pressed="true" />
<item android:drawable="@drawable/button_dialog_shape" />
</selector></span>
button_dialog_shape_pressed代码:
<span style="font-size:18px;"><shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/color_gray" />
</shape></span>
button_dialog_shape代码:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/color_white" />
</shape></span>
colors代码:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="color_blue">#2889d7</color>
<color name="color_white">#ffffff</color>
<color name="color_gray">#cccccc</color>
</resources></span>
点击按钮后的效果: