总是感觉dialog的系统样式很挫,不能满足APP的需求,当然更满足不了设计的要求了。
下面提供了一下自定义的dialog,仅供参考。
第一种:重写系统的方法
bbuton_danger_rounded:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"><shape>
<solid android:color="@color/bbutton_danger_pressed" />
<stroke android:width="1dp" android:color="@color/bbutton_danger_edge" />
<corners android:radius="@dimen/bbuton_rounded_corner_radius"/>
</shape></item>
<item android:state_enabled="false"><shape>
<solid android:color="@color/bbutton_danger_disabled" />
<stroke android:width="1dp" android:color="@color/bbutton_danger_disabled_edge" />
<corners android:radius="@dimen/bbuton_rounded_corner_radius"/>
</shape></item>
<item><shape>
<solid android:color="@color/bbutton_danger" />
<stroke android:width="1dp" android:color="@color/bbutton_danger_edge" />
<corners android:radius="@dimen/bbuton_rounded_corner_radius"/>
</shape></item>
</selector>
bg_bottom_custom_dialog:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item><shape>
<gradient android:angle="270" android:endColor="#e1e1e1" android:startColor="#e1e1e1" />
<corners android:bottomLeftRadius="15dp" android:bottomRightRadius="15dp" />
</shape></item>
</selector>
bg_title_custom_dialog:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item><shape>
<gradient android:angle="270"
android:endColor="#e1e1e1"
android:startColor="#e1e1e1" />
<corners android:topLeftRadius="15dp" android:topRightRadius="15dp" />
</shape></item>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minWidth="280dp"
android:orientation="vertical"
android:background="@android:color/transparent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_title_custom_dialog"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:orientation="horizontal" >
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:src="@drawable/ic_ico"
/>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:textSize="16sp"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="100dp"
android:background="@drawable/bg_middle_custom_dialog"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="16sp"
android:textColor="#FF000000"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:background="@drawable/bg_bottom_custom_dialog"
android:orientation="horizontal" >
<Button
android:id="@+id/positiveButton"
android:layout_width="0dp"
android:layout_marginLeft="30dp"
android:layout_height="wrap_content"
android:background="@drawable/bbuton_danger_rounded"
android:textColor="@color/bg_white"
android:layout_weight="1"
android:singleLine="true" />
<Button
android:id="@+id/negativeButton"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@drawable/bbuton_danger_rounded"
android:textColor="@color/bg_white"
android:layout_weight="1"
android:singleLine="true" />
</LinearLayout>
</LinearLayout>
<style name="load_dialog" parent="@android:style/Theme.Dialog">
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowFrame">@null</item><!--边框-->
<item name="android:windowIsFloating">true</item><!--是否浮现在activity之上-->
<item name="android:windowIsTranslucent">true</item><!--半透明-->
<item name="android:windowNoTitle">true</item><!--无标题-->
<!--<item name="android:background">@drawable/download</item><!–dialog背景样式–>-->
<item name="android:windowBackground">@android:color/transparent</item><!-- 背景透明 -->
<!--<item name="android:backgroundDimEnabled">false</item><!–模糊–>-->
</style>
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* Created by Administrator on 2015/4/21.
*/
public class MyDialog extends Dialog {
public MyDialog(Context context, int theme) {
super(context, theme);
}
public MyDialog(Context context) {
super(context);
}
/**
* Helper class for creating a custom dialog
*/
public static class Builder {
private Context context;
private String title;
private String message;
private String positiveButtonText;
private String negativeButtonText;
private View contentView;
private DialogInterface.OnClickListener positiveButtonClickListener, negativeButtonClickListener;
public Builder(Context context) {
this.context = context;
}
/**
* Set the Dialog message from String
* @param
* @return
*/
public Builder setMessage(String message) {
this.message = message;
return this;
}
/**
* Set the Dialog message from resource
* @param
* @return
*/
public Builder setMessage(int message) {
this.message = (String) context.getText(message);
return this;
}
/**
* Set the Dialog title from resource
* @param title
* @return
*/
public Builder setTitle(int title) {
this.title = (String) context.getText(title);
return this;
}
/**
* Set the Dialog title from String
* @param title
* @return
*/
public Builder setTitle(String title) {
this.title = title;
return this;
}
/**
* Set a custom content view for the Dialog.
* If a message is set, the contentView is not
* added to the Dialog...
* @param v
* @return
*/
public Builder setContentView(View v) {
this.contentView = v;
return this;
}
/**
* Set the positive button resource and it's listener
* @param positiveButtonText
* @param listener
* @return
*/
public Builder setPositiveButton(int positiveButtonText, DialogInterface.OnClickListener listener) {
this.positiveButtonText = (String) context.getText(positiveButtonText);
this.positiveButtonClickListener = listener;
return this;
}
/**
* Set the positive button text and it's listener
* @param positiveButtonText
* @param listener
* @return
*/
public Builder setPositiveButton(String positiveButtonText, DialogInterface.OnClickListener listener) {
this.positiveButtonText = positiveButtonText;
this.positiveButtonClickListener = listener;
return this;
}
/**
* Set the negative button resource and it's listener
* @param negativeButtonText
* @param listener
* @return
*/
public Builder setNegativeButton(int negativeButtonText, DialogInterface.OnClickListener listener) {
this.negativeButtonText = (String) context.getText(negativeButtonText);
this.negativeButtonClickListener = listener;
return this;
}
/**
* Set the negative button text and it's listener
* @param negativeButtonText
* @param listener
* @return
*/
public Builder setNegativeButton(String negativeButtonText, DialogInterface.OnClickListener listener) {
this.negativeButtonText = negativeButtonText;
this.negativeButtonClickListener = listener;
return this;
}
/**
* Create the custom dialog
*/
public MyDialog create() {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 给予Dialog
final MyDialog dialog = new MyDialog(context, R.style.load_dialog);
dialog.setCanceledOnTouchOutside(false);
View layout = inflater.inflate(R.layout.custom_dialog_layout, null);
dialog.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// set the dialog title
((TextView) layout.findViewById(R.id.title)).setText(title);
// set the confirm button
if (positiveButtonText != null) {
((Button) layout.findViewById(R.id.positiveButton)).setText(positiveButtonText);
if (positiveButtonClickListener != null) {
((Button) layout.findViewById(R.id.positiveButton)).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
}
});
}
} else {
// if no confirm button just set the visibility to GONE
layout.findViewById(R.id.positiveButton).setVisibility(View.GONE);
}
// set the cancel button
if (negativeButtonText != null) {
((Button) layout.findViewById(R.id.negativeButton)).setText(negativeButtonText);
if (negativeButtonClickListener != null) {
((Button) layout.findViewById(R.id.negativeButton)).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
}
});
}
} else {
// if no confirm button just set the visibility to GONE
layout.findViewById(R.id.negativeButton).setVisibility(View.GONE);
}
// set the content message
if (message != null) {
((TextView) layout.findViewById(R.id.message)).setText(message);
} else if (contentView != null) {
// if no message set
// add the contentView to the dialog body
((LinearLayout) layout.findViewById(R.id.content)).removeAllViews();
((LinearLayout) layout.findViewById(R.id.content)).addView(contentView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
dialog.setContentView(layout);
return dialog;
}
}
}
这样就完成了自己定义dialog的布局以及样式
第二种:用跳转Activity的方式制造一个dialog
原理:在跳转过去的Activity中用两个布局,一个是整体布局,设置透明度,第二个在第一个布局中居中,
然后dialog所要显示的内容放在第二个布局中,而且灵活性特别好。最后,外面的第一个布局点击事件
写上finish();就能完全模拟出来dialog的效果了。
<activity
android:name=".HongbaoShareActivity"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_hongbao_enter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#50000000"
android:gravity="center"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="90dp"
android:layout_marginLeft="27dp"
android:layout_marginRight="27dp"
android:layout_marginTop="90dp">
<LinearLayout
android:id="@+id/activity_hongbao_enter_neibu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="可以自定义一个背景,用drawable来画"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="12dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp">
<ImageView
android:id="@+id/hongbao_cishen_animation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:includeFontPadding="false"
android:src="@drawable/caishen_jishi_donghua" />
<TextView
android:id="@+id/hongbao_qiang_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/hongbao_enter_title_margin"
android:text="自定义"
android:textColor="@color/alpha_city_color"
android:textSize="16sp" />
</RelativeLayout>
<TextView
android:id="@+id/hongbao_qiang_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:gravity="center"
android:text="随意写"
android:textColor="#90ffffff"
android:textSize="16sp" />
<Button
android:id="@+id/hongbao_enter_qiang_btn"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="40dp"
android:layout_marginTop="10dp"
android:background="@drawable/circle_hongbao"
android:includeFontPadding="false"
android:text="抢"
android:textColor="@android:color/white"
android:textSize="27sp" />
</LinearLayout>
<ImageView
android:id="@+id/hongbao_common_offset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:layout_marginTop="6dp"
android:src="@drawable/hongbao_common_offset" />
</RelativeLayout>
</LinearLayout>
public class HongbaoEnterActivity extends Activity {
private int bid;
private long l;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hongbao_enter);
ImageView common = (ImageView) findViewById(R.id.hongbao_common_offset);
common.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
Intent intent = getIntent();
TextView title = (TextView)findViewById(R.id.hongbao_qiang_title);
TextView content = (TextView)findViewById(R.id.hongbao_qiang_txt);
title.setText(“自定义对话框”);
content.setText("啥啥啥");
Button btn = (Button) findViewById(R.id.hongbao_enter_qiang_btn);
LinearLayout enter = (LinearLayout) findViewById(R.id.activity_hongbao_enter);
enter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
LinearLayout enterNeibu = (LinearLayout) findViewById(R.id.activity_hongbao_enter_neibu);
enterNeibu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//不作处理
}
});
}
}