Android Button有默认padding值的元凶

2024-02-28 09:38

本文主要是介绍Android Button有默认padding值的元凶,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

刚给群里的小伙伴实现了一个自定义view,效果图如下

这里写图片描述

当我正准备开心地告诉小伙伴我已经实现好了的时候,这个b没装好哈~~~

为了跟小伙伴发的效果图达到几乎相似的效果,于是打算把button按钮样式也给写了(原谅我处女座哈!!)

就是控件最下方的那两个按钮,看到这个按钮样式的时候,小伙伴是不是也第一时间想到定义一个shape文件就好了呢,所以我就去定义了一个shape文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="false"><shape><corners android:radius="5dp"/><stroke android:color="#fff" android:width="1dp"/><solid android:color="#00000000"/></shape></item><item android:state_pressed="true"><shape><solid android:color="#00000000"/></shape></item><item><shape><corners android:radius="5dp"/><stroke android:color="#fff" android:width="1dp"/><solid android:color="#00000000"/></shape></item>
</selector>

然后运用在layout文件中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#a7ff0000"android:gravity="center_horizontal"android:orientation="vertical"><com.yasin.measuredemo.view.MeasureViewandroid:id="@+id/id_measure_view"android:layout_marginTop="40dp"android:layout_marginLeft="40dp"android:layout_marginRight="40dp"android:layout_width="match_parent"android:layout_height="wrap_content"/><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:layout_marginRight="20dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="0"android:textColor="#fff"android:textSize="14sp"android:layout_centerVertical="true"/><TextViewandroid:layout_centerInParent="true"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="最近一次测量:%1$s"android:textColor="#fff"android:textSize="15sp"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="150"android:textColor="#fff"android:textSize="14sp"android:layout_centerVertical="true"android:layout_alignParentRight="true"/></RelativeLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:layout_marginRight="20dp"android:layout_marginTop="10dp"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="单次测试"android:background="@drawable/selector_btn"android:textColor="#fff"android:textSize="14sp"/><Buttonandroid:layout_alignParentRight="true"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="单次测试"android:background="@drawable/selector_btn"android:textColor="#fff"android:textSize="14sp"/></RelativeLayout>
</LinearLayout>

这里写图片描述 
可以看到,左边那个按钮我什么都没加,然后貌似默认给了我一个padding值, 
于是我修改:

 <Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="单次测试"android:background="@drawable/selector_btn"android:textColor="#fff"android:textSize="14sp"android:padding="0dp"/>

把button的padding值设为了0,还是没用!!!

然后我就给button宽高定死了:

android:layout_width="100d p"android:layout_height="50dp"

是的!!!可以了~~~

但是我就是不想定死啊,这可咋办,于是想到了是不是系统默认给button设置的样式,于是打开了button源码:

 * {@link android.R.styleable#View View Attributes}* </p>*/
@RemoteView
public class Button extends TextView {public Button(Context context) {this(context, null);}public Button(Context context, AttributeSet attrs) {this(context, attrs, com.android.internal.R.attr.buttonStyle);}public Button(Context context, AttributeSet attrs, int defStyleAttr) {this(context, attrs, defStyleAttr, 0);}public Button(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {super(context, attrs, defStyleAttr, defStyleRes);}@Overridepublic CharSequence getAccessibilityClassName() {return Button.class.getName();}
}

代码很少,我们看到button默认的样式:

com.android.internal.R.attr.buttonStyle

于是带着怀疑去sdk25源码中找了一番:

<style name="Widget.Toolbar"><item name="titleTextAppearance">@style/TextAppearance.Widget.Toolbar.Title</item><item name="subtitleTextAppearance">@style/TextAppearance.Widget.Toolbar.Subtitle</item><item name="minHeight">?attr/actionBarSize</item><item name="titleMargin">4dp</item><item name="maxButtonHeight">@dimen/action_bar_default_height_material</item><item name="buttonGravity">top</item><item name="navigationButtonStyle">@style/Widget.Toolbar.Button.Navigation</item><item name="collapseIcon">?attr/homeAsUpIndicator</item><item name="collapseContentDescription">@string/toolbar_collapse_description</item><item name="contentInsetStart">16dp</item><item name="contentInsetStartWithNavigation">@dimen/action_bar_content_inset_with_nav</item><item name="touchscreenBlocksFocus">true</item></style><style name="Widget.Toolbar.Button.Navigation" parent="Widget"><item name="background">?attr/selectableItemBackground</item><item name="minWidth">56dp</item><item name="scaleType">center</item></style>

好吧,终于是找到元凶了,原来系统默认给button的最小值设置成了56dp,怪不得我咋设置高度跟padding都是这个高,唉唉!! 
于是对症下药,我们修改我们button的minheight为0:

<Buttonandroid:layout_alignParentRight="true"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="单次测试"android:background="@drawable/selector_btn"android:textColor="#fff"android:textSize="14sp"android:paddingLeft="10dp"android:paddingRight="10dp"android:paddingTop="5dp"android:paddingBottom="5dp"android:minHeight="0dp"/>

设置完android:minHeight=0dp后,就正常显示了。这算不算sdk25留的坑呢??

这篇关于Android Button有默认padding值的元凶的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/755099

相关文章

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

HTML5 中的<button>标签用法和特征

《HTML5中的<button>标签用法和特征》在HTML5中,button标签用于定义一个可点击的按钮,它是创建交互式网页的重要元素之一,本文将深入解析HTML5中的button标签,详细介绍其属... 目录引言<button> 标签的基本用法<button> 标签的属性typevaluedisabled

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

PostgreSQL 默认隔离级别的设置

《PostgreSQL默认隔离级别的设置》PostgreSQL的默认事务隔离级别是读已提交,这是其事务处理系统的基础行为模式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录一 默认隔离级别概述1.1 默认设置1.2 各版本一致性二 读已提交的特性2.1 行为特征2.2

Linux中修改Apache HTTP Server(httpd)默认端口的完整指南

《Linux中修改ApacheHTTPServer(httpd)默认端口的完整指南》ApacheHTTPServer(简称httpd)是Linux系统中最常用的Web服务器之一,本文将详细介绍如何... 目录一、修改 httpd 默认端口的步骤1. 查找 httpd 配置文件路径2. 编辑配置文件3. 保存

RedisTemplate默认序列化方式显示中文乱码的解决

《RedisTemplate默认序列化方式显示中文乱码的解决》本文主要介绍了SpringDataRedis默认使用JdkSerializationRedisSerializer导致数据乱码,文中通过示... 目录1. 问题原因2. 解决方案3. 配置类示例4. 配置说明5. 使用示例6. 验证存储结果7.

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

C++/类与对象/默认成员函数@构造函数的用法

《C++/类与对象/默认成员函数@构造函数的用法》:本文主要介绍C++/类与对象/默认成员函数@构造函数的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录名词概念默认成员函数构造函数概念函数特征显示构造函数隐式构造函数总结名词概念默认构造函数:不用传参就可以

C++类和对象之默认成员函数的使用解读

《C++类和对象之默认成员函数的使用解读》:本文主要介绍C++类和对象之默认成员函数的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、默认成员函数有哪些二、各默认成员函数详解默认构造函数析构函数拷贝构造函数拷贝赋值运算符三、默认成员函数的注意事项总结一