直播軟體開發,工具類的自定義彈窗效果

zhibo系統開發 發表於 2022-07-15

直播軟體開發,工具類的自定義彈窗效果

1.帶按鈕的彈出框(帶一個確定按鈕)

private void showDialog(String content){
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_licence,null,false);
        final AlertDialog dialog = new AlertDialog.Builder(mContext).setView(view).create();
 
        TextView btn_agree_high_opion = view.findViewById(R.id.tv_confirm);
        TextView tv_content = view.findViewById(R.id.tv_dialog_msg);
        tv_content.setText(content);
        btn_agree_high_opion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //... To-do
                dialog.dismiss();
            }
        });
        dialog.show();
        int width = mContext.getResources().getDisplayMetrics().widthPixels;
        //此處設定位置窗體大小,我這裡設定為了手機螢幕寬度的3/4  注意一定要在show方法呼叫後再寫設定視窗大小的程式碼,否則不起效果會
//        dialog.getWindow().setLayout((height/4*3), LinearLayout.LayoutParams.WRAP_CONTENT);
        Window win = dialog.getWindow();
        win.setGravity(Gravity.CENTER);   // 這裡控制彈出的位置
        win.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = width/2;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialog.getWindow().setBackgroundDrawable(null);
        win.setAttributes(lp);
}


佈局檔案示例(佈局檔案有些亂,只是個示例):

<LinearLayout xmlns:android="
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginLeft="@dimen/px30"
    android:layout_marginRight="@dimen/px30"
    android:background="@drawable/bg_radius_gray"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="@dimen/px100"
            android:gravity="center"
            android:orientation="vertical" >
            <TextView
                android:id="@+id/tv_dialog_msg"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/px20"
                android:gravity="center"
                android:textColor="@color/color_333333"
                android:textSize="@dimen/font_14" />
        </LinearLayout>
        <View
            android:layout_width="fill_parent"
            android:layout_marginHorizontal="@dimen/px20"
            android:layout_height="@dimen/px1"
            android:background="@color/color_515c74" />
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="bottom"
            android:orientation="horizontal" >
            <TextView
                android:id="@+id/tv_confirm"
                android:layout_width="fill_parent"
                android:layout_height="@dimen/px68"
                android:layout_weight="1"
                android:gravity="center"
                android:text="@string/common_confirm_text"
                android:textColor="@color/color_333333"
                android:textSize="@dimen/font_14" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>


2. 不帶按鈕,不用寫佈局檔案的彈出框

//3秒自動消失
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
//        builder.setTitle("傳送成功!");
        builder.setMessage(content);
        builder.setCancelable(true);
        final AlertDialog dlg = builder.create();
        dlg.show();
        final Timer t = new Timer();
        t.schedule(new TimerTask() {
            public void run() {
                dlg.dismiss();
                t.cancel();
            }
        }, 3000);
Window win = dlg.getWindow();
        win.setGravity(Gravity.CENTER);   // 這裡控制彈出的位置
//        win.getDecorView().setPadding(0, 0, 0, 0);
        int width = mContext.getResources().getDisplayMetrics().widthPixels;
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = width/5*3;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dlg.getWindow().setBackgroundDrawableResource(R.drawable.bg_radius_gray);
        win.setAttributes(lp);


以上就是 直播軟體開發,工具類的自定義彈窗效果,更多內容歡迎關注之後的文章


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2905947/,如需轉載,請註明出處,否則將追究法律責任。