美文网首页
Material 主题下Button的大小问题

Material 主题下Button的大小问题

作者: 100个大西瓜 | 来源:发表于2021-07-23 00:23 被阅读0次

    前端给了一些设计图,包括大小,字体颜色,背景颜色,字体大小;
    看起来挺合理的,但是实际上总是会出现显示不全:
    布局文件如下:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <Button
            android:layout_width="80dp"
            android:layout_height="30dp"
            android:textColor="@color/white"
            android:backgroundTint="@color/teal_700"
            android:text="按钮"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    效果图如下:


    效果图

    跟原型图差别过大;
    后来经过排查发现:主题使用了 Material 的

    parent="Theme.MaterialComponents.DayNight.DarkActionBar"
    

    这样子Button在布局加载时会被替换成 MaterialButton;
    而在该主题中,MaterialButton的样式是:

     <!-- Button styles -->
    <item name="buttonStyle">@style/Widget.AppCompat.Button</item>
    

    继续点击

        <!-- Bordered ink button -->
        <style name="Widget.Material.Button">
            <item name="background">@drawable/btn_default_material</item>
            <item name="textAppearance">?attr/textAppearanceButton</item>
            <item name="minHeight">48dip</item>
            <item name="minWidth">88dip</item>
            <item name="stateListAnimator">@anim/button_state_list_anim_material</item>
            <item name="focusable">true</item>
            <item name="clickable">true</item>
            <item name="gravity">center_vertical|center_horizontal</item>
        </style>
    

    继续追踪background的熟悉:

    <?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.
    -->
    
    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
            android:color="?attr/colorControlHighlight">
        <item android:drawable="@drawable/btn_default_mtrl_shape" />
    </ripple>
    

    继续追踪“@drawable/btn_default_mtrl_shape"

    <?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.
    -->
    
    <!-- Used as the canonical button shape. -->
    
    <inset xmlns:android="http://schemas.android.com/apk/res/android"
           android:insetLeft="@dimen/button_inset_horizontal_material"
           android:insetTop="@dimen/button_inset_vertical_material"
           android:insetRight="@dimen/button_inset_horizontal_material"
           android:insetBottom="@dimen/button_inset_vertical_material">
        <shape android:shape="rectangle"
               android:tint="?attr/colorButtonNormal">
            <corners android:radius="?attr/buttonCornerRadius" />
            <solid android:color="@color/white" />
            <padding android:left="@dimen/button_padding_horizontal_material"
                     android:top="@dimen/button_padding_vertical_material"
                     android:right="@dimen/button_padding_horizontal_material"
                     android:bottom="@dimen/button_padding_vertical_material" />
        </shape>
    </inset>
    

    其中

     <dimen name="button_inset_vertical_material">6dp</dimen>
    

    这就造成了按钮边框上下显示不全的问题。完全ojbk

    在Button中添加以下属性,即可去除背景边框

    android:insetTop="0dp"
    android:insetBottom="0dp"
    

    效果图如下:


    新的效果图

    ojbk

    相关文章

      网友评论

          本文标题:Material 主题下Button的大小问题

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