• 一手遮天 Android


    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

    一手遮天 Android - view(弹出类): AlertDialog 样式

    示例如下:

    /view/flyout/AlertDialogDemo4.java

    /**
     * AlertDialog 样式
     *
     * 本例演示如何通过 java 和 xml theme 修改 AlertDialog 的样式
     */
    
    package com.webabcd.androiddemo.view.flyout;
    
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.graphics.Color;
    import android.graphics.drawable.ColorDrawable;
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    import com.webabcd.androiddemo.R;
    
    import java.lang.reflect.Field;
    
    public class AlertDialogDemo4 extends AppCompatActivity {
    
        private Button mButton1;
        private Button mButton2;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_view_flyout_alertdialogdemo4);
    
            mButton1 = findViewById(R.id.button1);
            mButton2 = findViewById(R.id.button2);
    
            sample();
        }
    
        private void sample() {
            // 修改 AlertDialog 的样式(通过 java 修改)
            mButton1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogDemo4.this);
                    AlertDialog alert = builder
                            .setIcon(R.drawable.img_sample_son)
                            .setTitle("title")
                            .setMessage("message1
    message2")
                            .setPositiveButton("确定", null)
                            .create();
                    alert.show();
    
                    // 设置 AlertDialog 的背景颜色
                    alert.getWindow().setBackgroundDrawable(new ColorDrawable(Color.RED));
                    // 设置 AlertDialog 的 POSITIVE 按钮的文字颜色
                    alert.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(Color.GREEN);
    
                    // 通过反射修改 AlertDialog 的样式
                    try {
                        Field mAlert = AlertDialog.class.getDeclaredField("mAlert");
                        mAlert.setAccessible(true);
                        Object mAlertController = mAlert.get(alert);
    
                        // 设置 AlertDialog 的内容颜色
                        Field mMessage = mAlertController.getClass().getDeclaredField("mMessageView");
                        mMessage.setAccessible(true);
                        TextView mMessageView = (TextView) mMessage.get(mAlertController);
                        mMessageView.setTextColor(Color.BLUE);
    
                        // 设置 AlertDialog 的标题颜色
                        Field mTitle = mAlertController.getClass().getDeclaredField("mTitleView");
                        mTitle.setAccessible(true);
                        TextView mTitleView = (TextView) mTitle.get(mAlertController);
                        mTitleView.setTextColor(Color.YELLOW);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
    
            // 修改 AlertDialog 的样式(通过 xml theme 修改)
            mButton2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 第 2 个参数用于指定 AlertDialog 的样式(参见 values/styles.xml 中的 MyTheme_MyAlertDialogStyle)
                    AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogDemo4.this, R.style.MyTheme_MyAlertDialogStyle);
                    AlertDialog alert = builder
                            .setIcon(R.drawable.img_sample_son)
                            .setTitle("title")
                            .setMessage("message1
    message2")
                            .setPositiveButton("确定", null)
                            .create();
                    alert.show();
                }
            });
        }
    }
    
    

    /layout/activity_view_flyout_alertdialogdemo4.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAllCaps="false"
            android:text="修改 AlertDialog 的样式(通过 java 修改)" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAllCaps="false"
            android:text="修改 AlertDialog 的样式(通过 xml theme 修改)" />
    
    </LinearLayout>
    
    

    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

  • 相关阅读:
    response.redirect三种新页面转向方法
    respones.redirect 打开新的页面的两个方法
    临时表
    ODBC方式操作oracle数据库
    OLEDB方式操作oracle数据库
    oracle所有的乱码解决方案
    引用HM.Util.Ioc 的时候报错
    js webstrom中svn的配置及使用
    js vs2013中允许js访问json文件的解决方案
    Oracle 字符集常见字符集及解决方案
  • 原文地址:https://www.cnblogs.com/webabcd/p/android_view_flyout_AlertDialogDemo4.html
Copyright © 2020-2023  润新知