自定义AlertDialog,更改宽度,增加圆角

废话不多说,直接上代码:

 private void showLoginOutDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(ApplicationAgent.getAgent().getContext(), R.layout.kq_dialog_login_out_layout, null);
        dialog.setCancelable(false);
        dialog.setView(view);

        Window window = dialog.getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        TextView cancel = (TextView) view.findViewById(R.id.cancel_btn);
        TextView confirm = (TextView) view.findViewById(R.id.confirm_btn);
        cancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        confirm.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
                loginOutSystem();
            }
        });
        if (!dialog.isShowing())
            dialog.show();

        WindowManager.LayoutParams lp = window.getAttributes();
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {// 横屏
            lp.width = ScreenUtil.getScreenHeight(getApplicationContext()) / 10 * 8;
        } else {
            lp.width = ScreenUtil.getScreenWidth(getApplicationContext()) / 10 * 8;
        }
       window.setAttributes(lp);
    }

ScreenUtil工具类:

/**
     * 获取屏幕高度(px)
     */
    public static int getScreenHeight(Context context) {
        return context.getResources().getDisplayMetrics().heightPixels;
    }
    /**
     * 获取屏幕宽度(px)
     */
    public static int getScreenWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }

kq_dialog_login_out_layout.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/kq_common_dialog_bg"
                android:gravity="center">

    <ImageView
        android:id="@+id/warn_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="37dp"
        android:background="@color/kq_white"
        android:src="@drawable/kq_warning"/>

    <TextView
        android:id="@+id/warn_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/warn_img"
        android:layout_marginTop="@dimen/kq_dp20"
        android:background="@color/kq_white"
        android:gravity="center"
        android:text="@string/kq_sure_login_out"
        android:textColor="@color/kq_login_out_warn_color"
        android:textSize="16dp"/>

    <View
        android:id="@+id/a_line"
        style="@style/kq_horizontalLine"
        android:layout_below="@+id/warn_text"
        android:layout_marginTop="37dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_below="@+id/a_line"
        android:orientation="horizontal">

        <Button
            android:id="@+id/cancel_btn"
            style="?borderlessButtonStyle"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/kq_selector_button_left_bg"
            android:text="@string/kq_cancel"
            android:textColor="@color/kq_color_gray"
            android:textSize="@dimen/kq_dp16"/>

        <View
            android:layout_width="1px"
            android:layout_height="match_parent"
            android:background="@color/kq_bg_view"/>

        <Button
            android:id="@+id/confirm_btn"
            style="?borderlessButtonStyle"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/kq_selector_button_right_bg"
            android:text="@string/kq_confirm"
            android:textColor="@color/kq_theme_color_normal"
            android:textSize="@dimen/kq_dp16"/>
    </LinearLayout>
</RelativeLayout>

就酱紫,惊不惊喜,意不意外?点个赞吧!_

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容