Android的AlertDialog實現圓角邊框


第一步:一個圓角邊框背景文件:shape_bg_waring.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <!--&lt;!&ndash;描邊設置&ndash;&gt;-->
    <!--<stroke android:color="@android:color/darker_gray"-->
        <!--android:width="1px"-->
        <!--/>-->

    <!--填充設置-->
    <solid android:color="@android:color/white"/>

    <!--圓角設置-->
    <corners android:radius="15dp"/>

</shape>

第二步:在自定義的布局中的Layout中添加一條屬性:

 
        
android:background="@drawable/shape_bg_waring"
 
        

第三步:自定義AlertDialog的代碼

 // 構建dialog顯示的view布局
        View view = getLayoutInflater().from(this).inflate(R.layout.dialog_warning_layout, null);

        AlertDialog   dialog = new AlertDialog.Builder(this)
                .create();
        dialog.show();
        // 設置點擊可取消
        dialog.setCancelable(true);
       //給AlertDialog設置4個圓角
        dialog.getWindow().setBackgroundDrawableResource(R.drawable.shape_bg_waring);
        // 獲取Window對象
        Window window = dialog.getWindow();
        // 設置顯示視圖內容
        window.setContentView(view);
        TextView textView = view.findViewById(R.id.dialog_waring_title);
        textView.setText(content);
        TextView buttonText = view.findViewById(R.id.dialog_waring_text);
        buttonText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

 






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM