美文网首页
AlertDialog样式研究

AlertDialog样式研究

作者: b_e | 来源:发表于2020-12-10 20:43 被阅读0次

    默认样式

    <style name="AlertDialog.Material" parent="AlertDialog">
        <item name="fullDark">@empty</item>
        <item name="topDark">@empty</item>
        <item name="centerDark">@empty</item>
        <item name="bottomDark">@empty</item>
        <item name="fullBright">@empty</item>
        <item name="topBright">@empty</item>
        <item name="centerBright">@empty</item>
        <item name="bottomBright">@empty</item>
        <item name="bottomMedium">@empty</item>
        <item name="centerMedium">@empty</item>
        <item name="layout">@layout/alert_dialog_material</item>
        <item name="listLayout">@layout/select_dialog_material</item>
        <item name="progressLayout">@layout/progress_dialog_material</item>
        <item name="horizontalProgressLayout">@layout/alert_dialog_progress_material</item>
        <item name="listItemLayout">@layout/select_dialog_item_material</item>
        <item name="multiChoiceItemLayout">@layout/select_dialog_multichoice_material</item>
        <item name="singleChoiceItemLayout">@layout/select_dialog_singlechoice_material</item>
        <item name="controllerType">@integer/config_alertDialogController</item>
        <item name="selectionScrollOffset">@dimen/config_alertDialogSelectionScrollOffset</item>
    </style>
    
    名称 说明
    fullDark
    topDark
    centerDark
    bottomDark
    fullBright
    topBright
    centerBright
    bottomBright
    bottomMedium
    centerMedium
    layout 标准布局
    listLayout 列表布局
    progressLayout 圆形进度条布局
    horizontalProgressLayout 横方向进度条布局
    listItemLayout 列表项目布局
    multiChoiceItemLayout 多选列表项目布局
    singleChoiceItemLayout 单选列表项目布局
    controllerType Controller类型设置: 0 正常, 1 Micro
    selectionScrollOffset 设置滚动条位置

    标准布局

    <?xml version="1.0" encoding="utf-8"?>
    <!--
         Copyright (C) 2014 The Android Open Source Project
         Licensed under the Apache License, Version 2.0 (the "License");
         you may not use this file except in compliance with the License.
         You may obtain a copy of the License at
              http://www.apache.org/licenses/LICENSE-2.0
         Unless required by applicable law or agreed to in writing, software
         distributed under the License is distributed on an "AS IS" BASIS,
         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
         See the License for the specific language governing permissions and
         limitations under the License.
    -->
    
    <com.android.internal.widget.AlertDialogLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/parentPanel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="start|top"
        android:orientation="vertical">
    
        <include layout="@layout/alert_dialog_title_material" />
    
        <FrameLayout
            android:id="@+id/contentPanel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="48dp">
    
            <ScrollView
                android:id="@+id/scrollView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipToPadding="false">
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
    
                    <Space
                        android:id="@+id/textSpacerNoTitle"
                        android:visibility="gone"
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/dialog_padding_top_material" />
    
                    <TextView
                        android:id="@+id/message"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:paddingEnd="?attr/dialogPreferredPadding"
                        android:paddingStart="?attr/dialogPreferredPadding"
                        style="@style/TextAppearance.Material.Subhead" />
    
                    <Space
                        android:id="@+id/textSpacerNoButtons"
                        android:visibility="gone"
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/dialog_padding_top_material" />
                </LinearLayout>
            </ScrollView>
        </FrameLayout>
    
        <FrameLayout
            android:id="@+id/customPanel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="48dp">
    
            <FrameLayout
                android:id="@+id/custom"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </FrameLayout>
    
        <include
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            layout="@layout/alert_dialog_button_bar_material" />
    </com.android.internal.widget.AlertDialogLayout>
    
    名称 说明
    @layout/alert_dialog_title_material 标题布局
    dialogPreferredPadding 信息左右Padding
    @layout/alert_dialog_button_bar_material 按钮布局

    没有什么可以调整的,但凡有个图片啥的就使用setView()吧

    dialogPreferredPadding

    示例

    <style name="AppTheme.Dialog.Alert" parent="Theme.AppCompat.DayNight.Dialog.Alert">
        <item name="android:dialogPreferredPadding">@dimen/dialog_padding_0</item>
    </style>
    

    这个属性很多地方使用,慎改

    标题布局

    <?xml version="1.0" encoding="utf-8"?>
    <!--
         Copyright (C) 2015 The Android Open Source Project
         Licensed under the Apache License, Version 2.0 (the "License");
         you may not use this file except in compliance with the License.
         You may obtain a copy of the License at
              http://www.apache.org/licenses/LICENSE-2.0
         Unless required by applicable law or agreed to in writing, software
         distributed under the License is distributed on an "AS IS" BASIS,
         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
         See the License for the specific language governing permissions and
         limitations under the License.
    -->
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:id="@+id/topPanel"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical">
    
        <!-- If the client uses a customTitle, it will be added here. -->
    
        <LinearLayout
            android:id="@+id/title_template"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_vertical|start"
            android:paddingStart="?attr/dialogPreferredPadding"
            android:paddingEnd="?attr/dialogPreferredPadding"
            android:paddingTop="@dimen/dialog_padding_top_material">
    
            <ImageView
                android:id="@+id/icon"
                android:layout_width="32dip"
                android:layout_height="32dip"
                android:layout_marginEnd="8dip"
                android:scaleType="fitCenter"
                android:src="@null" />
    
            <com.android.internal.widget.DialogTitle
                android:id="@+id/alertTitle"
                android:singleLine="true"
                android:ellipsize="end"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textAlignment="viewStart"
                style="?attr/windowTitleStyle" />
        </LinearLayout>
    
        <Space
            android:id="@+id/titleDividerNoCustom"
            android:visibility="gone"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dialog_title_divider_material" />
    </LinearLayout>
    
    名称 说明
    dialogPreferredPadding 左右空白宽度
    windowTitleStyle 标题样式

    windowTitleStyle

    示例

    <style name="AppTheme.Dialog.Alert" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="android:windowTitleStyle">@style/DialogTitleStyle</item>
    </style>
    
    <style name="DialogTitleStyle" parent="RtlOverlay.DialogWindowTitle.AppCompat">
        <item name="android:textAppearance">@style/DialogTitleStyle.Title</item>
    </style>
    
    <style name="DialogTitleStyle.Title" parent="TextAppearance.AppCompat.Title">
        <item name="android:textSize">26sp</item>
        <item name="android:textColor">#FF0000</item>
    </style>
    

    只能调整文字大小和颜色,如果居中啥的,还是使用setCustomTitle()吧

    按钮布局

    <?xml version="1.0" encoding="utf-8"?>
    <!--
         Copyright (C) 2014 The Android Open Source Project
         Licensed under the Apache License, Version 2.0 (the "License");
         you may not use this file except in compliance with the License.
         You may obtain a copy of the License at
              http://www.apache.org/licenses/LICENSE-2.0
         Unless required by applicable law or agreed to in writing, software
         distributed under the License is distributed on an "AS IS" BASIS,
         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
         See the License for the specific language governing permissions and
         limitations under the License.
    -->
    
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/buttonPanel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scrollbarAlwaysDrawVerticalTrack="true"
                android:scrollIndicators="top|bottom"
                android:fillViewport="true"
                style="?attr/buttonBarStyle">
        <com.android.internal.widget.ButtonBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layoutDirection="locale"
            android:orientation="horizontal"
            android:paddingStart="12dp"
            android:paddingEnd="12dp"
            android:paddingTop="4dp"
            android:paddingBottom="4dp"
            android:gravity="bottom">
    
            <Button
                android:id="@+id/button3"
                style="?attr/buttonBarNeutralButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    
            <Space
                android:id="@+id/spacer"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:visibility="invisible" />
    
            <Button
                android:id="@+id/button2"
                style="?attr/buttonBarNegativeButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    
            <Button
                android:id="@+id/button1"
                style="?attr/buttonBarPositiveButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </com.android.internal.widget.ButtonBarLayout>
    </ScrollView>
    
    名称 说明
    buttonBarStyle 按钮容器样式
    buttonBarNeutralButtonStyle Neutral按钮样式
    buttonBarNegativeButtonStyle Negative按钮样式
    buttonBarPositiveButtonStyle Positive按钮样式

    按钮容器样式

    本身是一个ScrollView控件,按照ScrollView样式设置即可,其实也就是设置个空白啥的

    Positive按钮样式

    示例

    <style name="AppTheme.Dialog.Alert" parent="Theme.AppCompat.DayNight.Dialog.Alert">
        <item name="android:buttonBarPositiveButtonStyle">@style/DialogButtonStyle.Button.Positive</item>
    </style>
    
    <style name="DialogButtonStyle.Button.Positive">
        <item name="android:textColor">#000000</item>
        <item name="android:background">#00FF00</item>
        <item name="android:layout_weight">1</item>
    </style>
    

    按钮样式除了宽高和位置,其他都按照Button样式设置即可

    Negative按钮样式

    <style name="AppTheme.Dialog.Alert" parent="Theme.AppCompat.DayNight.Dialog.Alert">
        <item name="android:buttonBarNegativeButtonStyle">@style/DialogButtonStyle.Button.Negative</item>
    </style>
    
    <style name="DialogButtonStyle.Button.Negative">
        <item name="android:textColor">#000000</item>
        <item name="android:background">#00FFFF</item>
        <item name="android:layout_weight">1</item>
    </style>
    

    按钮样式除了宽高和位置,其他都按照Button样式设置即可

    Neutral按钮样式

    示例

    <style name="AppTheme.Dialog.Alert" parent="Theme.AppCompat.DayNight.Dialog.Alert">
        <item name="android:buttonBarNeutralButtonStyle">@style/DialogButtonStyle.Button.Neutral</item>
    </style>
    
    <style name="DialogButtonStyle.Button.Neutral">
        <item name="android:textColor">#000000</item>
        <item name="android:background">#FF00FF</item>
        <item name="android:textSize">24sp</item>
        <item name="android:layout_weight">1</item>
    </style>
    

    按钮样式除了宽高和位置,其他都按照Button样式设置即可

    列表布局

    <?xml version="1.0" encoding="utf-8"?>
    <!--
         Copyright (C) 2014 The Android Open Source Project
         Licensed under the Apache License, Version 2.0 (the "License");
         you may not use this file except in compliance with the License.
         You may obtain a copy of the License at
              http://www.apache.org/licenses/LICENSE-2.0
         Unless required by applicable law or agreed to in writing, software
         distributed under the License is distributed on an "AS IS" BASIS,
         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
         See the License for the specific language governing permissions and
         limitations under the License.
    -->
    
    <!--
        This layout file is used by the AlertDialog when displaying a list of items.
        This layout file is inflated and used as the ListView to display the items.
        Assign an ID so its state will be saved/restored.
    -->
    <view class="com.android.internal.app.AlertController$RecycleListView"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@id/select_dialog_listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="@null"
        android:divider="?attr/listDividerAlertDialog"
        android:scrollbars="vertical"
        android:overScrollMode="ifContentScrolls"
        android:textAlignment="viewStart"
        android:clipToPadding="false"
        android:paddingBottomNoButtons="@dimen/dialog_list_padding_bottom_no_buttons"
        android:paddingTopNoTitle="@dimen/dialog_list_padding_top_no_title" />
    
    名称 说明
    listDividerAlertDialog 分割符

    分割符

    示例

    <style name="AppTheme.Dialog.List" parent="Theme.AppCompat.DayNight.Dialog.Alert">
        <item name="android:listDividerAlertDialog">@drawable/dialog_list_divider</item>
    </style>
    

    分割符只识别drawable

    圆形进度条布局

    <?xml version="1.0" encoding="utf-8"?>
    <!--
         Copyright (C) 2014 The Android Open Source Project
         Licensed under the Apache License, Version 2.0 (the "License");
         you may not use this file except in compliance with the License.
         You may obtain a copy of the License at
              http://www.apache.org/licenses/LICENSE-2.0
         Unless required by applicable law or agreed to in writing, software
         distributed under the License is distributed on an "AS IS" BASIS,
         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
         See the License for the specific language governing permissions and
         limitations under the License.
    -->
    
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <LinearLayout
            android:id="@+id/body"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:baselineAligned="false"
            android:paddingStart="?attr/dialogPreferredPadding"
            android:paddingTop="@dimen/dialog_padding_top_material"
            android:paddingEnd="?attr/dialogPreferredPadding"
            android:paddingBottom="@dimen/dialog_padding_top_material">
    
            <ProgressBar
                android:id="@id/progress"
                style="?android:attr/progressBarStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:max="10000"
                android:layout_marginEnd="?attr/dialogPreferredPadding" />
    
            <TextView
                android:id="@+id/message"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical" />
        </LinearLayout>
    </FrameLayout>
    
    名称 说明
    dialogPreferredPadding 左右空白宽度
    progressBarStyle 进度条样式

    ProgressDialog已作废,这个好像没啥用了

    横方向进度条布局

    <?xml version="1.0" encoding="utf-8"?>
    <!--
         Copyright (C) 2014 The Android Open Source Project
         Licensed under the Apache License, Version 2.0 (the "License");
         you may not use this file except in compliance with the License.
         You may obtain a copy of the License at
              http://www.apache.org/licenses/LICENSE-2.0
         Unless required by applicable law or agreed to in writing, software
         distributed under the License is distributed on an "AS IS" BASIS,
         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
         See the License for the specific language governing permissions and
         limitations under the License.
    -->
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:paddingStart="?attr/dialogPreferredPadding"
        android:paddingTop="@dimen/dialog_padding_top_material"
        android:paddingEnd="?attr/dialogPreferredPadding"
        android:paddingBottom="@dimen/dialog_padding_top_material">
        <ProgressBar
            android:id="@+id/progress"
            style="?attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true" />
        <TextView
            android:id="@+id/progress_percent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@id/progress" />
        <TextView
            android:id="@+id/progress_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_below="@id/progress" />
    </RelativeLayout>
    
    名称 说明
    dialogPreferredPadding 左右空白宽度
    progressBarStyleHorizontal 进度条样式

    ProgressDialog已作废,这个好像没啥用了

    列表项目布局

    <?xml version="1.0" encoding="utf-8"?>
    <!--
         Copyright (C) 2014 The Android Open Source Project
         Licensed under the Apache License, Version 2.0 (the "License");
         you may not use this file except in compliance with the License.
         You may obtain a copy of the License at
              http://www.apache.org/licenses/LICENSE-2.0
         Unless required by applicable law or agreed to in writing, software
         distributed under the License is distributed on an "AS IS" BASIS,
         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
         See the License for the specific language governing permissions and
         limitations under the License.
    -->
    
    <!--
        This layout file is used by the AlertDialog when displaying a list of items.
        This layout file is inflated and used as the TextView to display individual
        items.
    -->
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:textColor="?android:attr/textColorAlertDialogListItem"
        android:gravity="center_vertical"
        android:paddingStart="?attr/listPreferredItemPaddingStart"
        android:paddingEnd="?attr/listPreferredItemPaddingEnd"
        android:ellipsize="marquee" />
    
    名称 说明
    listPreferredItemHeightSmall 最小高度
    textAppearanceListItemSmall 文字外观
    textColorAlertDialogListItem 文字颜色
    listPreferredItemPaddingStart 左空白宽度
    listPreferredItemPaddingEnd 右空白宽度

    示例

    <style name="AppTheme.Dialog.List" parent="Theme.AppCompat.DayNight.Dialog.Alert">
        <item name="android:listPreferredItemHeightSmall">72dp</item>
        <item name="android:textColorAlertDialogListItem">#666666</item>
        <item name="android:listPreferredItemPaddingStart">64dp</item>
        <item name="android:listPreferredItemPaddingEnd">32dp</item>
        <item name="android:textAppearanceListItemSmall">@style/TextAppearance.AppCompat.Small</item>
    </style>
    

    想要项目居中显示的话,还是使用setView()吧

    多选列表项目布局

    <?xml version="1.0" encoding="utf-8"?>
    <!--
         Copyright (C) 2014 The Android Open Source Project
         Licensed under the Apache License, Version 2.0 (the "License");
         you may not use this file except in compliance with the License.
         You may obtain a copy of the License at
              http://www.apache.org/licenses/LICENSE-2.0
         Unless required by applicable law or agreed to in writing, software
         distributed under the License is distributed on an "AS IS" BASIS,
         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
         See the License for the specific language governing permissions and
         limitations under the License.
    -->
    
    <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/listPreferredItemHeightSmall"
        android:textAppearance="?attr/textAppearanceMedium"
        android:textColor="?attr/textColorAlertDialogListItem"
        android:gravity="center_vertical"
        android:paddingStart="@dimen/select_dialog_padding_start_material"
        android:paddingEnd="?attr/dialogPreferredPadding"
        android:drawableStart="?attr/listChoiceIndicatorMultiple"
        android:drawablePadding="@dimen/select_dialog_drawable_padding_start_material"
        android:ellipsize="marquee" />
    
    名称 说明
    listPreferredItemHeightSmall 最小高度
    textAppearanceMedium 文字外观
    textColorAlertDialogListItem 文字颜色
    dialogPreferredPadding 右空白宽度
    listChoiceIndicatorMultiple 选择框样式

    示例

    <style name="AppTheme.Dialog.List" parent="Theme.AppCompat.DayNight.Dialog.Alert">
        <item name="android:listPreferredItemHeightSmall">72dp</item>
        <item name="android:textColorAlertDialogListItem">#666666</item>
        <item name="android:dialogPreferredPadding">32dp</item>
        <item name="android:textAppearanceMedium">@style/TextAppearance.AppCompat.Medium</item>
        <item name="android:listChoiceIndicatorMultiple">@drawable/imagegrid_delete_image</item>
    </style>
    

    listChoiceIndicatorMultiple

    可以是图片

    单选列表项目布局

    <?xml version="1.0" encoding="utf-8"?>
    <!--
         Copyright (C) 2014 The Android Open Source Project
         Licensed under the Apache License, Version 2.0 (the "License");
         you may not use this file except in compliance with the License.
         You may obtain a copy of the License at
              http://www.apache.org/licenses/LICENSE-2.0
         Unless required by applicable law or agreed to in writing, software
         distributed under the License is distributed on an "AS IS" BASIS,
         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
         See the License for the specific language governing permissions and
         limitations under the License.
    -->
    
    <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/listPreferredItemHeightSmall"
        android:textAppearance="?attr/textAppearanceMedium"
        android:textColor="?attr/textColorAlertDialogListItem"
        android:gravity="center_vertical"
        android:paddingStart="@dimen/select_dialog_padding_start_material"
        android:paddingEnd="?attr/dialogPreferredPadding"
        android:drawableStart="?attr/listChoiceIndicatorSingle"
        android:drawablePadding="@dimen/select_dialog_drawable_padding_start_material"
        android:ellipsize="marquee" />
    
    名称 说明
    listPreferredItemHeightSmall 最小高度
    textAppearanceMedium 文字外观
    textColorAlertDialogListItem 文字颜色
    dialogPreferredPadding 右空白宽度
    listChoiceIndicatorSingle 选择框样式

    示例

    <style name="AppTheme.Dialog.List" parent="Theme.AppCompat.DayNight.Dialog.Alert">
        <item name="android:listPreferredItemHeightSmall">72dp</item>
        <item name="android:textColorAlertDialogListItem">#666666</item>
        <item name="android:dialogPreferredPadding">32dp</item>
        <item name="android:textAppearanceMedium">@style/TextAppearance.AppCompat.Medium</item>
        <item name="android:listChoiceIndicatorSingle">@drawable/imagegrid_delete_image</item>
    </style>
    

    listChoiceIndicatorSingle

    可以是图片

    相关文章

      网友评论

          本文标题:AlertDialog样式研究

          本文链接:https://www.haomeiwen.com/subject/gwvhgktx.html