自定义Alertdialog设置style ,避免在不同手机和平台之间的差异,可设置 Icon 、title、 message、
view、positiButton 、negativeButton等,也可根据需要自行扩展
- /* dialog 实现类 */
- import com.android.R;
- import com.android.app.AlertDialogImpl.Builder.Parameters;
- import android.app.Activity;
- import android.os.Bundle;
- import android.support.v4.app.DialogFragment;
- import android.support.v4.app.FragmentActivity;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.FrameLayout;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- public class AlertDialogImpl extends DialogFragment{
-
- public static class Builder {
-
- private FragmentActivity mActivity;
-
- public Builder(FragmentActivity activity) {
- this.mActivity = activity;
- }
-
- public Builder setIcon(int iconId) {
- Parameters.icon = iconId;
- return this;
- }
-
- public Builder setTitle(String title) {
- Parameters.title = title;
- return this;
- }
-
- public Builder setMessage(String message) {
- Parameters.message = message;
- return this;
- }
-
- public Builder setView(View view) {
- Parameters.view = view;
- return this;
- }
- /* 注意此处监听为onclicklistener */
- public Builder setPositive(CharSequence chars, OnClickListener mpListener) {
- Parameters.possitiveChars = chars;
- Parameters.mpListener = mpListener;
- return this;
- }
-
- public Builder setNegative(CharSequence chars, OnClickListener mnListener) {
- Parameters.negativeChars = chars;
- Parameters.mnListener = mnListener;
- return this;
- }
-
- public AlertDialogImpl show() {
- AlertDialogImpl impl = new AlertDialogImpl();
- impl.show(mActivity.getSupportFragmentManager(), "DialogBuilder");
- return impl;
- }
-
- public static class Parameters {
- static String title = "";
- static int icon = -1;
- static String message = "";
- static View view;
- static CharSequence possitiveChars = "", negativeChars = "";
- static OnClickListener mpListener, mnListener;
- }
- }
-
- @Override
- public void onAttach(Activity activity) {
- // TODO Auto-generated method stub
- super.onAttach(activity);
- Log.e("tag", "onattach");
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- Log.e("tag", "onCreate");
- setStyle(0, R.style.style_self_dialog_toast);
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- Log.e("tag", "onCreateview");
-
- View view = inflater.inflate(R.layout.alertdialog_view, container, false);
- ImageView icon = (ImageView)view.findViewById(R.id.dialog_icon);
- if(Parameters.icon == -1) {
- icon.setVisibility(View.GONE);
- }else {
- icon.setBackgroundResource(Parameters.icon);
- }
-
-
- TextView title = (TextView)view.findViewById(R.id.dialog_title);
- int visibility = Parameters.title == null || Parameters.title.equals("") ? View.GONE : View.VISIBLE;
- LinearLayout titlLinear = (LinearLayout)view.findViewById(R.id.dialog_title_linear);
- titlLinear.setVisibility(visibility);
- CharSequence charSequence = Parameters.title == null ? "" : Parameters.title;
- title.setText(charSequence);
-
- TextView message = (TextView)view.findViewById(R.id.dialog_message);
- message.setText(Parameters.message);
-
- FrameLayout mView = (FrameLayout)view.findViewById(R.id.dialog_middle_view);
- if(Parameters.view == null) {
- mView.setVisibility(View.GONE);
- }else {
- mView.addView(Parameters.view);
- }
-
- Button btnPossitive = (Button)view.findViewById(R.id.dialog_sure);
- Button btnNegavite = (Button)view.findViewById(R.id.dialog_cancel);
-
- if(Parameters.mpListener == null) {
- btnPossitive.setVisibility(View.GONE);
- }else {
- btnPossitive.setText(Parameters.possitiveChars);
- btnPossitive.setOnClickListener(Parameters.mpListener);
- }
- if(Parameters.mnListener == null) {
- btnNegavite.setVisibility(View.GONE);
- }else {
- btnNegavite.setText(Parameters.negativeChars);
- btnNegavite.setOnClickListener(Parameters.mnListener);
- }
- if(Parameters.mpListener == null && Parameters.mnListener == null) {
- ((LinearLayout)view.findViewById(R.id.dialog_linear_bottom)).setVisibility(View.GONE);
- }
- return view;
- }
- }
- /* dialog view布局 */
- <?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"
- android:padding="5dp" >
- <LinearLayout
- android:id="@+id/dialog_title_linear"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:paddingLeft="10dp"
- android:gravity="left|center_vertical"
- android:orientation="horizontal"
- android:background="@drawable/dialog_top_holo_light" >
- <ImageView
- android:id="@+id/dialog_icon"
- android:layout_width="45dp"
- android:layout_height="match_parent"
- android:background="@drawable/ic_launcher"
- android:contentDescription="@string/app_name"/>
-
- <TextView
- android:id="@+id/dialog_title"
- android:layout_width="match_parent"
- android:layout_height="45dp"
- android:layout_marginLeft="5dp"
- android:gravity="left|center_vertical"
- android:paddingBottom="5dp"
- android:paddingTop="5dp"
- android:textColor="#000"
- android:textSize="22sp" />
- </LinearLayout>
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="1dp"
- android:background="@drawable/divider_horizontal_bright"
- android:contentDescription="@string/app_name" />
- <FrameLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/dialog_middle_holo_light" >
- <TextView
- android:id="@+id/dialog_message"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:textColor="#000"
- android:paddingTop="15dp"
- android:paddingBottom="15dp"
- android:textSize="18sp" />
- <FrameLayout
- android:id="@+id/dialog_middle_view"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:minHeight="30dp" />
- </FrameLayout>
- <LinearLayout
- android:id="@+id/dialog_linear_bottom"
- android:layout_width="match_parent"
- android:layout_height="48dp"
- android:background="@drawable/dialog_bottom_holo_light"
- android:orientation="horizontal" >
- <Button
- android:id="@+id/dialog_sure"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:layout_marginRight="0dp"
- android:background="@drawable/btn" />
- <Button
- android:id="@+id/dialog_cancel"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:background="@drawable/btn" />
- </LinearLayout>
- </LinearLayout>
- /* dialog style */
- <style name="style_self_dialog_toast" parent="@android:style/Theme.Dialog">
- <item name="android:windowFrame">@null</item>
- <item name="android:windowBackground">@drawable/dialog_bg_color</item>
- <item name="android:windowIsTranslucent">true</item>
- <item name="android:windowNoTitle">true</item>
- <item name="android:windowIsFloating">true</item>
- <item name="android:minHeight">60dp</item>
- <item name="android:maxHeight">400dp</item>
- <item name="android:minWidth">240dp</item>
- <item name="android:maxWidth">280dp</item>
- </style>
- /* 应用 */
- public class ThridActivity extends FragmentActivity{
- public AlertDialogImpl impl;
- @Override
- protected void onCreate(Bundle arg0) {
- // TODO Auto-generated method stub
- super.onCreate(arg0);
- setContentView(R.layout.tabwidgets_layout);
- ((TextView)findViewById(R.id.tabwidget_indicator)).setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- View view = (View)getLayoutInflater().inflate(R.layout.progress_dialog_view, null);
- impl = new AlertDialogImpl.Builder(ThridActivity.this)
- .setTitle("温馨提示")
- .setView(view)
- .setPositive("确定", new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Log.e("onclick", "sure");
- impl.dismiss();
- }
- })
- .setNegative("取消", new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Log.e("onclick", "cancel");
- impl.dismiss();
- }
- })
- .show();
- }
- });
- }
- }
复制代码 |
|