简单自定义Dialog类,百分比中的应用

场景

结合百分比布局,如何自定义Dialog的方式。

基础Dialog类

package XXX;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.support.annotation.LayoutRes;
import android.text.Layout;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.XXX.interfaces.IForViewChooser;
import com.XXX.utils.LogUtils;
import com.XXX.views.ViewShowChooser;

public class BaseDialog extends Dialog {
    protected final static int ALPHA_MAIN_DIALOG_BG = 0x77;
    Context mContext;
    Resources mResources;
    int mRootWidth = 0;
    int mRootHeight = 0;
    View mRoot;
    protected float mAlpha = ALPHA_MAIN_DIALOG_BG;
    int mScreenWidth;
    int mScreenHeight;
    protected ViewShowChooser mViewShowChooser;
    protected IForViewChooser mIForViewChooser;
    public BaseDialog(Context context,ViewShowChooser viewShowChooser) {
        super(context);
        setViewChooser(viewShowChooser);
        mContext = context;
        mResources = mContext.getResources();
    }
    public BaseDialog(Context context) {
        super(context);
        mContext = context;
        mResources = mContext.getResources();
    }
    private void setViewChooser(ViewShowChooser viewShowChooser) {
        mViewShowChooser = viewShowChooser;
        mIForViewChooser = mViewShowChooser.getIForViewChooser();
    }
    
     void init(@LayoutRes int id) {
        View inflate = LayoutInflater.from(mContext).inflate(id, null);
        mRoot = inflate;
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setContentView(inflate);
        getScreenParams();
    }
    

    private void getScreenParams() {
        DisplayMetrics displayMetrics = mResources.getDisplayMetrics();
        mScreenWidth = displayMetrics.widthPixels;
        mScreenHeight = displayMetrics.heightPixels;
    }
    
    public BaseDialog(Context context, int themeResId) {
        super(context, themeResId);
    }
    
    public BaseDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }
    
    @Override
    public void show() {
        super.show();
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width =mRootWidth;
        params.height =mRootHeight;
        params.alpha = mAlpha;
        getWindow().setAttributes(params);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    }
}

子类实现

package XXX;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.XXX.R;
import com.XXX.interfaces.IViewShowChooser;
import com.XXX.utils.LogUtils;
import com.XXX.views.ViewShowChooser;

public class FavorChooseDialog extends BaseDialog implements View.OnClickListener{
    private final static double WIDTH_PERCENT = 0.25;
    private final static double HEIGHT_PERCENT = 0.2925925925925926;
    private int mCurrentChannelIndexForFavorDialog=0;
    private Resources mResources;
    public FavorChooseDialog(Context context,int currentChannelIndexForFavorDialog,ViewShowChooser viewShowChooser) {
        super(context,viewShowChooser);
        init(R.layout.dialog_favor);
        mResources = context.getResources();
        mRootWidth = (int) (mScreenWidth * WIDTH_PERCENT);
        mRootHeight = (int) (mScreenHeight * HEIGHT_PERCENT);
        LogUtils.d(LogUtils.TAG,"FavorChooseDialog--FavorChooseDialog mRootWidth="+mRootWidth+" mRootHeight="+mRootHeight);
        mCurrentChannelIndexForFavorDialog = currentChannelIndexForFavorDialog;
        initView();
        
    }
    private Button mBtnFavorite;
    private Button mBtnCancel;
    private int currentIndexFavorState;
    private void initView() {
        currentIndexFavorState= mIForViewChooser.getChannelFavor(mCurrentChannelIndexForFavorDialog);//1 favor
        mBtnFavorite = mRoot.findViewById(R.id.btn_favorite);
        mBtnCancel = mRoot.findViewById(R.id.btn_cancel);
        if (currentIndexFavorState == 1) {
            mBtnFavorite.setText(mResources.getString(R.string.channel_unfavorite));
        }
        mBtnFavorite.setOnClickListener(this);
        mBtnCancel.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_favorite:
                if (currentIndexFavorState == 1) {
                    mIForViewChooser.setChannelFavor(mCurrentChannelIndexForFavorDialog, 0);
                    mBtnFavorite.setText(mResources.getString(R.string.channel_favor));
                } else {
                    mIForViewChooser.setChannelFavor(mCurrentChannelIndexForFavorDialog, 1);
                    mBtnFavorite.setText(mResources.getString(R.string.channel_unfavorite));
                }
                break;
            case R.id.btn_cancel:
                mViewShowChooser.hideFavorChooseDialog();
                break;
        }
    }
}

布局例子

<?xml version="1.0" encoding="utf-8"?>
<com.xxx.views.percent.PercentRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/prl_favor_dialog_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorDialogMainBG"
    >
    <Button
        android:id="@+id/btn_favorite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="@drawable/dialog_selector_parent_control_btn_bg"
        android:includeFontPadding="false"
        android:text="Favorite"
        android:textAllCaps="false"

        app:layout_textSizePercent="12%h"
        app:layout_marginTopPercent="16.77215189873418%h"
        app:layout_heightPercent="23.73417721518987%h"
        app:layout_widthPercent="75%w"
        />
    <Button
        android:id="@+id/btn_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="@drawable/dialog_selector_parent_control_btn_bg"
        android:includeFontPadding="false"
        android:text="Cancel"
        android:textAllCaps="false"

        app:layout_textSizePercent="12%h"
        app:layout_marginTopPercent="59.49367088607595%h"
        app:layout_heightPercent="23.73417721518987%h"
        app:layout_widthPercent="75%w"
        />
</com.xxx.views.percent.PercentRelativeLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值