安卓—自定義 AlertDialog 的樣式

自定義修改安卓彈出框的樣式java

效果圖:android

 

 

1.在style.xml下添加ide

<!-- 自定義彈出樣式 -->
    <style name="MyDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <!--是否浮在窗口之上-->
        <!--<item name="android:windowIsFloating">true</item>-->
        <!--半透明-->
        <!--<item name="android:windowIsTranslucent">true</item>-->
        <!--是否顯示title-->
        <item name="android:windowNoTitle">true</item>
        <!--dialog以外沒有焦點的區域是否罩上黑色半透明-->
        <item name="android:background">@color/white</item>
        <item name="android:textColor">@color/gray</item>
        <item name="android:textSize">@dimen/f24</item>
        <!-- 這裏是修改頂部標題背景顏色,具體顏色本身定,能夠是圖片 -->
        <item name="android:topDark">@color/blue_alert</item>
        <!-- 這裏是修改內容區域背景顏色 -->
        <item name="android:layout_gravity">center</item>
        <item name="buttonBarButtonStyle">@color/white</item>
    </style>

 2.在主體配置裏引入自定義的AlertDialog主題函數

3.java代碼寫法ui

/**
     * 默認的彈出窗口
     * @param view
     */
    public void alertDialog(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("彈出窗");
        builder.setMessage("提示信息!");
        builder.setPositiveButton("確認", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                System.out.println("確認執行函數");
            }
        }).setNegativeButton("取消", null);
        builder.show();
    }

    /**
     * 自定義樣式的彈出窗
     * @param view
     */
    public void alertDialog2(View view) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        // 自定義 title樣式
        TextView title = new TextView(this);
        title.setText("自定義彈出窗");
        title.setTextSize(24);
        title.setGravity(Gravity.CENTER);
        title.setPadding(0,20,0,20);
        builder.setCustomTitle(title);
        // 中間的信息以一個view的形式設置進去
        TextView msg = new TextView(this);
        msg.setText("自定義彈出提示信息");
        msg.setTextSize(24);
        msg.setGravity(Gravity.CENTER);
        msg.setPadding(20, 40, 20, 40);
        builder.setView(msg);

        builder.setPositiveButton("確認", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                System.out.println("確認執行函數");
            }
        }).setNegativeButton("取消", null);
        // 調用 show()方法後獲得 dialog對象
        AlertDialog dialog = builder.show();
        final Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
        final Button negativeButton=dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
        LinearLayout.LayoutParams positiveParams =(LinearLayout.LayoutParams)positiveButton.getLayoutParams();
        positiveParams.gravity = Gravity.CENTER;
        positiveParams.setMargins(10,10,10,10);
        positiveParams.width = 0;
        // 安卓下面有三個位置的按鈕,默認權重爲 1,設置成 500或更大才能讓兩個按鈕看起來均分
        positiveParams.weight = 500;
        LinearLayout.LayoutParams negativeParams =(LinearLayout.LayoutParams)negativeButton.getLayoutParams();
        negativeParams.gravity = Gravity.CENTER;
        negativeParams.setMargins(10,10,10,10);
        negativeParams.width = 0;
        negativeParams.weight = 500;
        positiveButton.setLayoutParams(positiveParams);
        negativeButton.setLayoutParams(negativeParams);
        positiveButton.setBackgroundColor(Color.parseColor("#FF733E"));
        positiveButton.setTextColor(Color.parseColor("#FFFFFF"));
        negativeButton.setBackgroundColor(Color.parseColor("#DDDDDD"));
    }
相關文章
相關標籤/搜索