2015-12-13 16:20:00 qq_16430735 阅读数 2748

46.Android 自定义Dialog


前言

提供两套自定义Dialog模板

  • 第一种,提示Dialog,有消失时间。

  • 第二种,菜单Dialog,用于用户交互。


提示Dialog

CustomDialog

public class CustomDialog extends Dialog {

    private TextView dialogTV;

    private static final long DEFAULT_DURATION = 1000L;
    private static final String DEFAULT_CONTENT = "";

    private long duration;
    private String content;

    private DialogCallback callback;

    public CustomDialog(Context context) {
        super(context, R.style.custom_dialog);
        this.initViews(context);
    }

    /**
     * Creates a dialog window that uses a custom dialog style.
     * <p/>
     * The supplied {@code context} is used to obtain the window manager and
     * base theme used to present the dialog.
     * <p/>
     * The supplied {@code theme} is applied on top of the context's theme. See
     * <a href="{@docRoot}guide/topics/resources/available-resources.html#stylesandthemes">
     * Style and Theme Resources</a> for more information about defining and
     * using styles.
     *
     * @param context    the context in which the dialog should run
     * @param themeResId a style resource describing the theme to use for the
     *                   window, or {@code 0} to use the default dialog theme
     */
    public CustomDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.initViews(context);
    }

    public CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.initViews(context);
    }

    private void initViews(Context context) {
        this.setContentView(R.layout.dialog_custom);
        this.dialogTV = (TextView) this.findViewById(R.id.custom_dialog_tv);
    }

    @Override
    public void show() {
        super.show();
        this.dialogTV.setText(TextUtils.isEmpty(this.content) ? DEFAULT_CONTENT : this.content);
        long showDuration = this.duration > 0L ? this.duration : DEFAULT_DURATION;
        new Handler().postDelayed(new Runnable() {
            public void run() {
                if (CustomDialog.this.isShowing()) {
                    CustomDialog.this.dismiss();
                    if (CustomDialog.this.callback != null) CustomDialog.this.callback.onDismiss();
                }
            }
        }, showDuration);
    }

    public void setTextDrawable(Drawable drawable) {
        if (drawable == null) return;
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        this.dialogTV.setCompoundDrawables(drawable, null, null, null);
    }

    public interface DialogCallback {
        void onDismiss();
    }

    public static class DialogBuilder {
        private static String contextHashCode;
        private static CustomDialog dialog;
        public static DialogBuilder ourInstance;

        public static DialogBuilder getInstance(Context context) {
            if (ourInstance == null) ourInstance = new DialogBuilder();
            String hashCode = String.valueOf(context.hashCode());
            /**
             * 不同一个Activity
             */
            if (!hashCode.equals(String.valueOf(contextHashCode))) {
                contextHashCode = hashCode;
                dialog = new CustomDialog(context);
            }
            return ourInstance;
        }

        public DialogBuilder setDuration(long duration) {
            dialog.duration = duration;
            return this;
        }

        public DialogBuilder setContent(String content) {
            dialog.content = content;
            return this;
        }

        public DialogBuilder setDrawable(Drawable drawable) {
            dialog.setTextDrawable(drawable);
            return this;
        }

        public DialogBuilder setCallback(DialogCallback callback) {
            dialog.callback = callback;
            return this;
        }

        public DialogBuilder setCanceledOnTouchOutside(boolean cancel) {
            dialog.setCanceledOnTouchOutside(cancel);
            return this;
        }

        public CustomDialog getDialog() {
            return dialog;
        }

    }

}

dialog_custom.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:gravity="center">

    <TextView
        android:id="@+id/custom_dialog_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_dialog_custom_tv"
        android:drawableLeft="@mipmap/dialog_custom_tv_drawable"
        android:drawablePadding="5dp"
        android:drawableStart="@mipmap/dialog_custom_tv_drawable"
        android:gravity="center"
        android:text="成功"
        android:textColor="#ff666666"
        android:textSize="15sp" />
</LinearLayout>

R.style.custom_dialog

<style name="custom_dialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@color/transparent</item>
    <item name="android:windowIsTranslucent">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowIsFloating">true</item>
</style>

提示Dialog 效果图

custom_dialog


菜单Dialog

MenuDialog

public class MenuDialog extends Dialog {

    private TextView caseTV;
    private TextView helpTV;

    public MenuDialog(Context context) {
        super(context, R.style.menu_dialog);
        this.initViews(context);
    }

    /**
     * Creates a dialog window that uses a custom dialog style.
     * <p/>
     * The supplied {@code context} is used to obtain the window manager and
     * base theme used to present the dialog.
     * <p/>
     * The supplied {@code theme} is applied on top of the context's theme. See
     * <a href="{@docRoot}guide/topics/resources/available-resources.html#stylesandthemes">
     * Style and Theme Resources</a> for more information about defining and
     * using styles.
     *
     * @param context    the context in which the dialog should run
     * @param themeResId a style resource describing the theme to use for the
     *                   window, or {@code 0} to use the default dialog theme
     */
    public MenuDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.initViews(context);
    }

    public MenuDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.initViews(context);
    }

    private void initViews(Context context) {
        this.setContentView(R.layout.dialog_menu);
        this.caseTV = (TextView) this.findViewById(R.id.dialog_menu_case_tv);
        this.helpTV = (TextView) this.findViewById(R.id.dialog_menu_help_tv);
    }


    public void setTextDrawable(Drawable drawable) {
        if (drawable == null) return;
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
    }

    public static class DialogBuilder {
        private static String contextHashCode;
        private static MenuDialog dialog;
        public static DialogBuilder ourInstance;

        public static DialogBuilder getInstance(Context context) {
            if (ourInstance == null) ourInstance = new DialogBuilder();
            String hashCode = String.valueOf(context.hashCode());
            /**
             * 不同一个Activity
             */
            if (!hashCode.equals(String.valueOf(contextHashCode))) {
                contextHashCode = hashCode;
                dialog = new MenuDialog(context);
            }
            return ourInstance;
        }

        public DialogBuilder setCaseListenser(View.OnClickListener listener) {
            dialog.caseTV.setOnClickListener(listener);
            return this;
        }

        public DialogBuilder setHelpListener(View.OnClickListener listener) {
            dialog.helpTV.setOnClickListener(listener);
            return this;
        }

        public DialogBuilder setCanceledOnTouchOutside(boolean cancel) {
            dialog.setCanceledOnTouchOutside(cancel);
            return this;
        }

        public MenuDialog getDialog() {
            return dialog;
        }

    }

}

dialog_menu.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:layout_gravity="center"
    android:gravity="center"
    android:orientation="horizontal">


    <TextView
        android:id="@+id/dialog_menu_case_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_dialog_menu_item"
        android:drawablePadding="13dp"
        android:drawableTop="@mipmap/dialog_menu_case"
        android:paddingBottom="13.5dp"
        android:paddingEnd="36dp"
        android:paddingLeft="36dp"
        android:paddingRight="36dp"
        android:paddingTop="20dp"
        android:gravity="center_horizontal"
        android:text="Case"
        android:textColor="#ff666666"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/dialog_menu_help_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:background="@drawable/bg_dialog_menu_item"
        android:drawablePadding="13dp"
        android:drawableTop="@mipmap/dialog_menu_help"
        android:gravity="center_horizontal"
        android:paddingBottom="13.5dp"
        android:paddingEnd="36dp"
        android:paddingLeft="36dp"
        android:paddingRight="36dp"
        android:paddingTop="20dp"
        android:text="Help"
        android:textColor="#ff666666"
        android:textSize="14sp" />

</LinearLayout>

R.style.menu_dialog

<style name="menu_dialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@color/transparent</item>
    <item name="android:windowIsTranslucent">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowIsFloating">true</item>
</style>

菜单Dialog 效果图

menu_dialog


DialogActivity

public class DialogActivity extends AppCompatActivity implements View.OnClickListener {

    private MenuDialog menuDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_dialog);
        this.menuDialog = MenuDialog.DialogBuilder.getInstance(this)
                .setCaseListenser(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(DialogActivity.this, "case", Toast.LENGTH_SHORT).show();
                        DialogActivity.this.menuDialog.dismiss();
                    }
                })
                .setHelpListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(DialogActivity.this, "Help", Toast.LENGTH_SHORT).show();
                        DialogActivity.this.menuDialog.dismiss();
                    }
                })
                .getDialog();
        this.initListeners();
    }

    private void initListeners() {
        this.findViewById(R.id.dialog_custom).setOnClickListener(this);
        this.findViewById(R.id.dialog_menu).setOnClickListener(this);
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_custom:
                CustomDialog.DialogBuilder.getInstance(this)
                        .setDuration(2000L)
                        .setContent("CustomDialog")
                        .setCanceledOnTouchOutside(false)
                        .setCallback(new CustomDialog.DialogCallback() {
                            @Override
                            public void onDismiss() {
                                Toast.makeText(DialogActivity.this, "CustomDialog dismiss", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .getDialog()
                        .show();
                break;
            case R.id.dialog_menu:
                this.menuDialog.show();
                break;
        }
    }
}

activity_dialog.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"
    android:padding="26dp">

    <TextView
        android:id="@+id/dialog_custom"
        style="@style/TextButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CustomDialog" />

    <TextView
        android:id="@+id/dialog_menu"
        style="@style/TextButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="MenuDialog" />

</LinearLayout>
2016-03-15 17:41:07 luciferVSme 阅读数 1651

仿ios风格的Android 分享dialog

Android 开发人员都知道,很多时候Android应用都会在设计上仿照或者迁就ios版本。尤其在系统自带风的控件上(比如timepicker,dialog等等)。网上一直没有成熟的仿ios9样式的分享dialog,然而只能自己做啦,现在分享给大家,希望大神们多批评~~·

先上效果图这里写图片描述

说下思路:先要想办法 查询手机内所有支持分享的应用列表,拿到他们的classname,icon等等东西,再绑定到dilaog的HorizontalListView上,最后用系统级的ACTION_SEND分享你要分享的内容。
至于如何查询手机内所有支持分享的应用列表有篇文章:http://blog.csdn.net/wangjia55/article/details/8285403
大神已经说得很清楚小弟也是借鉴而已。

好啦 下面上代码。

先上MainActivity

package com.exmple.shareapp.shareapplikeios;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btnAndroid;
    private Button btnIos;
    private HorizontalListView h_list_view;
    HorizontalListViewAdapter h_adapter;
    ArrayList<AppInfo> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        list = getShareAppList();
    }

    private void initView() {

        btnAndroid = (Button) findViewById(R.id.android_btn);
        btnIos = (Button) findViewById(R.id.ios_btn);
        btnAndroid.setOnClickListener(this);
        btnIos.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.android_btn:
                Intent data = new Intent(Intent.ACTION_SEND);
                data.setData(Uri.parse("mailto:"));
                data.setType("text/plain");
                data.putExtra(Intent.EXTRA_TEXT, "www.baidu.com");
                startActivity(data);
                break;

            case R.id.ios_btn:
                showDialog();
                break;
        }

    }

    private void showDialog() {

        final Dialog d = Utility.getDialog(this, R.layout.dialog_share_app);
        Window dialogWindow = d.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        dialogWindow.setGravity(Gravity.BOTTOM);
        dialogWindow.setAttributes(lp);

        initDialog(d);

        d.show();

    }

    private void initDialog(final Dialog d) {
        // cyl
        h_list_view = (HorizontalListView) d.findViewById(R.id.h_list_view);
        h_adapter = new HorizontalListViewAdapter(this, R.layout.cell_share_horizontal_list_item, list);
        h_list_view.setAdapter(h_adapter);
        h_list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent shareIntent = new Intent(Intent.ACTION_SEND);
                AppInfo appInfo = (AppInfo) parent.getItemAtPosition(position);
                shareIntent.setComponent(new ComponentName(appInfo.getPkgName(), appInfo.getLaunchClassName()));
                shareIntent.setType("text/plain");
                shareIntent.putExtra(Intent.EXTRA_TEXT, "www.baidu.com");
                startActivity(shareIntent);
            }
        });

        Button btnDismiss = (Button) d.findViewById(R.id.btn_dismiss);
        btnDismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                d.dismiss();
            }
        });
    }


    private ArrayList<AppInfo> getShareAppList() {
        ArrayList<AppInfo> shareAppInfos = new ArrayList<>();
        PackageManager packageManager = getPackageManager();
        List<ResolveInfo> resolveInfos = getShareApps(MainActivity.this);
        if (null == resolveInfos) {
            return null;
        } else {
            for (ResolveInfo resolveInfo : resolveInfos) {
                AppInfo appInfo = new AppInfo();
                appInfo.setPkgName(resolveInfo.activityInfo.packageName);
                appInfo.setLaunchClassName(resolveInfo.activityInfo.name);
                appInfo.setAppName(resolveInfo.loadLabel(packageManager).toString());
                appInfo.setAppIcon(resolveInfo.loadIcon(packageManager));
                shareAppInfos.add(appInfo);
            }
        }

        return shareAppInfos;
    }

    public List<ResolveInfo> getShareApps(Context context) {
        List<ResolveInfo> mApps;
        Intent intent = new Intent(Intent.ACTION_SEND, null);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setType("text/plain");
        PackageManager pManager = context.getPackageManager();
        mApps = pManager.queryIntentActivities(intent,
                PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
        return mApps;
    }

}

接着是HorizontalListView当然这也是借鉴某位大神的,侵删。。。



package com.exmple.shareapp.shareapplikeios;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.Scroller;

import java.util.LinkedList;
import java.util.Queue;

public class HorizontalListView extends AdapterView<ListAdapter> {

    public boolean mAlwaysOverrideTouch = true;
    protected ListAdapter mAdapter;
    private int mLeftViewIndex = -1;
    private int mRightViewIndex = 0;
    protected int mCurrentX;
    protected int mNextX;
    private int mMaxX = Integer.MAX_VALUE;
    private int mDisplayOffset = 0;
    protected Scroller mScroller;
    private GestureDetector mGesture;
    private Queue<View> mRemovedViewQueue = new LinkedList<View>();
    private OnItemSelectedListener mOnItemSelected;
    private OnItemClickListener mOnItemClicked;
    private OnItemLongClickListener mOnItemLongClicked;
    private boolean mDataChanged = false;


    public HorizontalListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private synchronized void initView() {
        mLeftViewIndex = -1;
        mRightViewIndex = 0;
        mDisplayOffset = 0;
        mCurrentX = 0;
        mNextX = 0;
        mMaxX = Integer.MAX_VALUE;
        mScroller = new Scroller(getContext());
        mGesture = new GestureDetector(getContext(), mOnGesture);
    }

    @Override
    public void setOnItemSelectedListener(OnItemSelectedListener listener) {
        mOnItemSelected = listener;
    }

    @Override
    public void setOnItemClickListener(OnItemClickListener listener){
        mOnItemClicked = listener;
    }

    @Override
    public void setOnItemLongClickListener(OnItemLongClickListener listener) {
        mOnItemLongClicked = listener;
    }

    private DataSetObserver mDataObserver = new DataSetObserver() {

        @Override
        public void onChanged() {
            synchronized(HorizontalListView.this){
                mDataChanged = true;
            }
            invalidate();
            requestLayout();
        }

        @Override
        public void onInvalidated() {
            reset();
            invalidate();
            requestLayout();
        }

    };

    @Override
    public ListAdapter getAdapter() {
        return mAdapter;
    }

    @Override
    public View getSelectedView() {
        //TODO: implement
        return null;
    }

    @Override
    public void setAdapter(ListAdapter adapter) {
        if(mAdapter != null) {
            mAdapter.unregisterDataSetObserver(mDataObserver);
        }
        mAdapter = adapter;
        mAdapter.registerDataSetObserver(mDataObserver);
        reset();
    }

    private synchronized void reset(){
        initView();
        removeAllViewsInLayout();
        requestLayout();
    }

    @Override
    public void setSelection(int position) {
        //TODO: implement
    }

    private void addAndMeasureChild(final View child, int viewPos) {
        LayoutParams params = child.getLayoutParams();
        if(params == null) {
            params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        }

        addViewInLayout(child, viewPos, params, true);
        child.measure(MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST),
                MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.AT_MOST));
    }



    @Override
    protected synchronized void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        if(mAdapter == null){
            return;
        }

        if(mDataChanged){
            int oldCurrentX = mCurrentX;
            initView();
            removeAllViewsInLayout();
            mNextX = oldCurrentX;
            mDataChanged = false;
        }

        if(mScroller.computeScrollOffset()){
            int scrollx = mScroller.getCurrX();
            mNextX = scrollx;
        }

        if(mNextX <= 0){
            mNextX = 0;
            mScroller.forceFinished(true);
        }
        if(mNextX >= mMaxX) {
            mNextX = mMaxX;
            mScroller.forceFinished(true);
        }

        int dx = mCurrentX - mNextX;

        removeNonVisibleItems(dx);
        fillList(dx);
        positionItems(dx);

        mCurrentX = mNextX;

        if(!mScroller.isFinished()){
            post(new Runnable(){
                @Override
                public void run() {
                    requestLayout();
                }
            });

        }
    }

    private void fillList(final int dx) {
        int edge = 0;
        View child = getChildAt(getChildCount()-1);
        if(child != null) {
            edge = child.getRight();
        }
        fillListRight(edge, dx);

        edge = 0;
        child = getChildAt(0);
        if(child != null) {
            edge = child.getLeft();
        }
        fillListLeft(edge, dx);


    }

    private void fillListRight(int rightEdge, final int dx) {
        while(rightEdge + dx < getWidth() && mRightViewIndex < mAdapter.getCount()) {

            View child = mAdapter.getView(mRightViewIndex, mRemovedViewQueue.poll(), this);
            addAndMeasureChild(child, -1);
            rightEdge += child.getMeasuredWidth();

            if(mRightViewIndex == mAdapter.getCount()-1) {
                mMaxX = mCurrentX + rightEdge - getWidth();
            }

            if (mMaxX < 0) {
                mMaxX = 0;
            }
            mRightViewIndex++;
        }

    }

    private void fillListLeft(int leftEdge, final int dx) {
        while(leftEdge + dx > 0 && mLeftViewIndex >= 0) {
            View child = mAdapter.getView(mLeftViewIndex, mRemovedViewQueue.poll(), this);
            addAndMeasureChild(child, 0);
            leftEdge -= child.getMeasuredWidth();
            mLeftViewIndex--;
            mDisplayOffset -= child.getMeasuredWidth();
        }
    }

    private void removeNonVisibleItems(final int dx) {
        View child = getChildAt(0);
        while(child != null && child.getRight() + dx <= 0) {
            mDisplayOffset += child.getMeasuredWidth();
            mRemovedViewQueue.offer(child);
            removeViewInLayout(child);
            mLeftViewIndex++;
            child = getChildAt(0);

        }

        child = getChildAt(getChildCount()-1);
        while(child != null && child.getLeft() + dx >= getWidth()) {
            mRemovedViewQueue.offer(child);
            removeViewInLayout(child);
            mRightViewIndex--;
            child = getChildAt(getChildCount()-1);
        }
    }

    private void positionItems(final int dx) {
        if(getChildCount() > 0){
            mDisplayOffset += dx;
            int left = mDisplayOffset;
            for(int i=0;i<getChildCount();i++){
                View child = getChildAt(i);
                int childWidth = child.getMeasuredWidth();
                child.layout(left, 0, left + childWidth, child.getMeasuredHeight());
                left += childWidth + child.getPaddingRight();
            }
        }
    }

    public synchronized void scrollTo(int x) {
        mScroller.startScroll(mNextX, 0, x - mNextX, 0);
        requestLayout();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        boolean handled = super.dispatchTouchEvent(ev);
        handled |= mGesture.onTouchEvent(ev);
        return handled;
    }

    protected boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                              float velocityY) {
        synchronized(HorizontalListView.this){
            mScroller.fling(mNextX, 0, (int)-velocityX, 0, 0, mMaxX, 0, 0);
        }
        requestLayout();

        return true;
    }

    protected boolean onDown(MotionEvent e) {
        mScroller.forceFinished(true);
        return true;
    }

    private GestureDetector.OnGestureListener mOnGesture = new GestureDetector.SimpleOnGestureListener() {

        @Override
        public boolean onDown(MotionEvent e) {
            return HorizontalListView.this.onDown(e);
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                               float velocityY) {
            return HorizontalListView.this.onFling(e1, e2, velocityX, velocityY);
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                                float distanceX, float distanceY) {

            synchronized(HorizontalListView.this){
                mNextX += (int)distanceX;
            }
            requestLayout();

            return true;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            for(int i=0;i<getChildCount();i++){
                View child = getChildAt(i);
                if (isEventWithinView(e, child)) {
                    if(mOnItemClicked != null){
                        mOnItemClicked.onItemClick(HorizontalListView.this, child, mLeftViewIndex + 1 + i, mAdapter.getItemId( mLeftViewIndex + 1 + i ));
                    }
                    if(mOnItemSelected != null){
                        mOnItemSelected.onItemSelected(HorizontalListView.this, child, mLeftViewIndex + 1 + i, mAdapter.getItemId( mLeftViewIndex + 1 + i ));
                    }
                    break;
                }

            }
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            int childCount = getChildCount();
            for (int i = 0; i < childCount; i++) {
                View child = getChildAt(i);
                if (isEventWithinView(e, child)) {
                    if (mOnItemLongClicked != null) {
                        mOnItemLongClicked.onItemLongClick(HorizontalListView.this, child, mLeftViewIndex + 1 + i, mAdapter.getItemId(mLeftViewIndex + 1 + i));
                    }
                    break;
                }

            }
        }

        private boolean isEventWithinView(MotionEvent e, View child) {
            Rect viewRect = new Rect();
            int[] childPosition = new int[2];
            child.getLocationOnScreen(childPosition);
            int left = childPosition[0];
            int right = left + child.getWidth();
            int top = childPosition[1];
            int bottom = top + child.getHeight();
            viewRect.set(left, top, right, bottom);
            return viewRect.contains((int) e.getRawX(), (int) e.getRawY());
        }
    };

}

还有对应的HorizontalListViewAdapter


package com.exmple.shareapp.shareapplikeios;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class HorizontalListViewAdapter extends ArrayAdapter<AppInfo> {

    private Context mContext;
    private ArrayList<AppInfo> appList;
    private int mResource;

    public HorizontalListViewAdapter(Context context, int resource, ArrayList<AppInfo> objects){
        super(context, resource, objects);
        this.mResource = resource;
        this.mContext = context;
        this.appList = objects;
    }

    @Override
    public int getCount() {
        return appList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final AppInfo appInfo = getItem(position);
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(mResource, null);

        ImageView img = (ImageView) convertView.findViewById(R.id.img_list_item);
        TextView txt =(TextView) convertView.findViewById(R.id.text_list_item);

        txt.setText(appInfo.getAppName());
        img.setBackground(appInfo.getAppIcon());

        return convertView;
    }
}

当然还有重要的app对象类



package com.exmple.shareapp.shareapplikeios;

import android.graphics.drawable.Drawable;

public class AppInfo {

    private String pkgName;
    private String launchClassName;
    private String appName;
    private Drawable appIcon;

    public String getPkgName() {
        return pkgName;
    }

    public void setPkgName(String pkgName) {
        this.pkgName = pkgName;
    }

    public String getLaunchClassName() {
        return launchClassName;
    }

    public void setLaunchClassName(String launchClassName) {
        this.launchClassName = launchClassName;
    }

    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public Drawable getAppIcon() {
        return appIcon;
    }

    public void setAppIcon(Drawable appIcon) {
        this.appIcon = appIcon;
    }
}

好了 基本上重要的代码都在这了。希望以后自己的代码越来越好~~
希望能跟大家多多交流~~
最后整个demo下载地址在这:http://download.csdn.net/detail/lucifervsme/9462330

就酱~~拜拜

2016-02-28 00:37:22 xiang_freedom 阅读数 2545

Android自定义Dialog的一些问题

怎样自定义Dialog

Android自带的Dialog不管是样式还是功能都比较普通。如果有比较特殊的需求,比如自定义的布局、不需要标题栏、不需要按钮以及多个dialog页面切换,就需要自定义Dialog。

自定义Dialog一般是对Dialog的重写,比如继承一个AlertDialog类。然后重写onCreate方法,把自定义的布局加进去即可,同时设置布局元素的事件响应。
dialog在onCreate方法中设置不显示标题栏:
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

一般自定义dialog的事件响应都会给Activity一个回调:

package com.demo;   

 import android.app.Dialog;   
 import android.content.Context;   
 import android.os.Bundle;   
 import android.view.View;   
 import android.view.View.OnClickListener;   
 import android.widget.Button;   

 public class LeaveMeetingDialog extends Dialog  implements OnClickListener{   

 private Button  quitBtn,stopBtn,cancelBtn;   
 private LeaveMeetingDialogListener listener;   

 public interface LeaveMeetingDialogListener{   
     public void onClick(View view);   
 }   

 public LeaveMeetingDialog(Context context,int theme,LeaveMeetingDialogListener listener) {   
     super(context,theme);   
 }   

 @Override   
 protected void onCreate(Bundle savedInstanceState) {   
     super.onCreate(savedInstanceState);   
     setContentView(R.layout.leave_meeting_dialog);   
     initViews();   
 }   

 private void initViews(){   
     quitBtn = (Button)findViewById(R.id.quit_btn);   
     stopBtn = (Button)findViewById(R.id.stop_btn);   
     cancelBtn = (Button)findViewById(R.id.cancel_btn);   

     quitBtn.setOnClickListener(this);   
     stopBtn.setOnClickListener(this);   
     cancelBtn.setOnClickListener(this);   
 }   

 @Override   
 public void onClick(View v) {   
     listener.onClick(v);   
 }   

 } 
---调用处:  

[java]  
 LeaveMeetingDialog  dialog = new LeaveMeetingDialog(this,R.style.Theme_CustomDialog,   
             new LeaveMeetingDialogListener() {   
         @Override   
         public void onClick(View view) {   
             switch(view.getId()){   
                 case R.id.quit_btn:break;   
                 case R.id.stop_btn:break;   
                 case R.id.cancel_btn:break;   
             }   
         }   
 });   
       dialog.show();

代码来源:http://www.cnblogs.com/rainly/p/3435110.html

自定义Dialog一些特殊设置

平常设置AlertDialog一些功能,比如点击外部区域Dialog不消失、点击返回键也不消失等,都是通过AlertDialog的Builder来完成。自定义Dialog如果继承自AlertDialog,按理说也可以以这样的方式:

builder.setTitle(getText(R.string.XXXX))
                        .setMessage(getText(R.XXXXXX))
                        .setOnKeyListener(key)//返回键监听器
                        .setCancelable(false)//设置点击外部区域dialog不消失
                        .setPositiveButton(android.R.string.ok, someOKButtonListener)
                        .setNegativeButton(android.R.string.cancel, null);
 OnKeyListener keylistener = new DialogInterface.OnKeyListener(){
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (keyCode==KeyEvent.KEYCODE_BACK&&event.getRepeatCount()==0)
            {
             return true;//返回true会截断返回键的点击事件,不做其他处理
            }
            else
            {
             return false;
            }
        }
    } ;

代码来源:http://www.apkbus.com/android-114253-1-1.html

但是实际上没有作用。而是应该设置:

dialog.setCancelable(false);
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                    @Override
                    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                        if (keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                });

背景虚化

Android在API 14以后不再支持Dialog背景虚化。参见
下面的代码没有作用。

setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

WindowManager.LayoutParams lp = dialog.getWindow().getAttributes(); 
lp.dimAmount = 0.55f; //0f背景亮度不变,1f背景变黑
dialog.getWindow().setAttributes(lp); 
dialog.getWindow() 
.addFlags(WindowManager.LayoutParams.FLAGDIMBEHIND);

只是实现背景亮度改变。

2013-10-12 10:00:12 yuanyuan_186 阅读数 588

        

        分类:             Android           660人阅读     评论(1)     收藏     举报    
  1. WindowManager.LayoutParams layoutParams = getWindow().window.getAttributes();  
  2. layoutParams.width = WINDOW_WIDTH;  //修改窗体宽高  
  3. layoutParams.height = WINDOW_HEIGHT;  
  4. layoutParams.alpha = WINDOW_ALPHA;      //修改窗体透明度  
  5. layoutParams.x = x; //修改窗体显示的位置  
  6. layoutParams.y = y;  
  7. 需要注意的是,窗体显示的初始位置是窗体的中心在屏幕的中心位置上,此时layoutParams.x和layoutParams.y都是0。  
2016-11-23 22:15:11 lastyuan 阅读数 69

Android自定义Dialog

Android本身提供的类不能满足我们开发需要时,我们就可以自定义Dialog,和大多数自定义控件一样,自定义的Dialog的步骤也是继承与接口调用等特征:

1.自定义一个MyDialog类继承致Dialog

    在这里简单的说明性,我为什么要继承Dialog而不是AlertDialog,因为继承dialog的自定义对话框它可以占满我们整个屏幕的宽度,而继承AlertDialog就会在自定义对话框的两侧有间距,当然我们也可以设置参数使得继承了AlertDialog的自定义对话框也能占满整个屏幕宽度,但继承了dialog就不用而外的设置:
public class MyDialog extends Dialog {

    public MyDialog(Context context) {
        this(context,R.style.quick_option_dialog);//这里改为this,为了实现自己的MyDialog(Context context, int themeResId)方法,并且要传入一个自定义的样式文件
    }
    public MyDialog(Context context, int themeResId) {
        super(context, themeResId);
    }
}

这个自定义的样式文件(style)继承致Theme.Holo.Light.Dialog:

 <!-- 定义了dialog的进出动画 -->
 <style name="dialog_animation" parent="android:Animation">
        <item name="@android:windowEnterAnimation">@anim/dialog_enter</item>
        <item name="@android:windowExitAnimation">@anim/dialog_exit</item>
    </style>

    <!--quick_option_dialog的样式 ,背景和动画,当然还可以定义更多属性,根据自己的需求进行定义 -->
    <style name="quick_option_dialog" parent="android:Theme.Holo.Light.Dialog">
        <item name="android:windowBackground">@color/white40</item>
        <item name="android:windowAnimationStyle">@style/dialog_animation</item>
    </style>

实现样式文件(style)中的动画:

<!-- 定义了dialog的进出动画:@anim/dialog_exit,从父控件的100%->自身零 -->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="200"
        android:toYDelta="0%"
        android:fillAfter="true"
        android:fromYDelta="100%p" />

</set>
<!-- 定义了dialog的进出动画:@anim/dialog_enter,从自身零->父控件的100% -->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="200"
        android:fromYDelta="0%"
        android:fillAfter="true"
        android:toYDelta="100%p" />

</set>

2. 初始化view对象,最终setContentView(view):

    初始化我们自定义的view,并设置给Dialog:
public MyDialog(Context context, int theme) {
        super(context, theme);
        //初始化view
        View contentView = getLayoutInflater().inflate(R.layout.dialog_1, null);
            ll_hide = contentView.findViewById(R.id.ll_hide);
            ll_show = contentView.findViewById(R.id.ll_show);
            tv_unmber = (EditText) contentView.findViewById(R.id.tv_unmber);
            tv_password = (EditText) contentView.findViewById(R.id.tv_password);

        tv_unmber.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                ll_show.setVisibility(View.VISIBLE);
                ll_hide.setVisibility(View.GONE);
                return false;
            }
        });
        tv_password.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                ll_hide.setVisibility(View.VISIBLE);
                ll_show.setVisibility(View.GONE);
                return false;
            }
        });
        super.setContentView(contentView);  
    }

自定义的view:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:background="#f2f2f2"
              android:id="@+id/ll_dialog"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@drawable/ic_vip_profile_background">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:textSize="25dp"
            android:textColor="#fff"
            android:paddingLeft="20dp"
            android:layout_centerVertical="true"
            android:text="@string/dialog_title"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:textSize="25dp"
            android:textColor="#fff"
            android:paddingRight="20dp"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:text="@string/dialog_title2"/>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:id="@+id/ll_show"
            android:paddingTop="40dp"
            android:layout_width="match_parent"
            android:background="#f2f2f2"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:scaleType="fitXY"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_22"/>
            <ImageView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_bili_logo"/>
            <ImageView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:scaleType="fitXY"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_33"/>

        </LinearLayout>
        <LinearLayout
            android:id="@+id/ll_hide"
            android:visibility="gone"
            android:paddingTop="40dp"
            android:layout_width="match_parent"
            android:background="#f2f2f2"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_22_hide"/>
            <ImageView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_bili_logo"/>
            <ImageView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_33_hide"/>

        </LinearLayout>
    </RelativeLayout>
    <LinearLayout
        android:background="#f2f2f2"
        android:paddingLeft="40dp"
        android:paddingTop="10dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_login_username_default"/>
        <EditText
            android:id="@+id/tv_unmber"
            android:layout_width="280dp"
            android:text="@string/dialog_number"
            android:layout_height="wrap_content"/>
       <!-- <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_login_password_default"/>
        <EditText
            android:layout_width="280dp"
            android:text="@string/dialog_password"
            android:layout_height="wrap_content"/>-->
    </LinearLayout>
    <LinearLayout
        android:background="#f2f2f2"
        android:paddingLeft="40dp"
        android:paddingTop="10dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
         <ImageView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:src="@drawable/ic_login_password_default"/>
         <EditText
             android:id="@+id/tv_password"
             android:layout_width="280dp"
             android:password="true"
             android:text="@string/dialog_password"
             android:layout_height="wrap_content"/>
    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        >
        <Button

            android:background="@drawable/selector_background"
            android:padding="10dp"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:textColor="@drawable/selector_register"
            android:text="@string/dialog_register"/>
        <Button
            android:layout_alignParentRight="true"
            android:gravity="center_horizontal"
            android:background="@drawable/selector_background"
            android:padding="10dp"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:textColor="@drawable/selector_register"
            android:text="@string/dialog_enter"/>
    </RelativeLayout>

</LinearLayout>

3.指定对话框出现的位置:

在MyDialog中重写onCreate方法:

@Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        //设置位置在底部
        getWindow().setGravity(Gravity.BOTTOM);

        //如果继承Dialog可以省略一下步骤,如果继承AlertDialog并想要保证对话框占满宽度就需要设置此参数
        WindowManager m = getWindow().getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        p.width = d.getWidth();
        getWindow().setAttributes(p);
    }

4.调用对话框

这样的一个自定义对话框就定义好了,这时就是调用的关系了,需要在哪里调用这样的对话框:

MyDialog dialog = new MyDialog(MainActivity.this);
                dialog.setCancelable(true);//设置对话框可以取消
                dialog.setCanceledOnTouchOutside(true);//设置对话框点击外部可以取消
                dialog.show();

下面是效果图:
这里写图片描述这里写图片描述这里写图片描述

源代码这里写链接内容

没有更多推荐了,返回首页