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

相关文章

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++类和对象之默认成员函数的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、默认成员函数有哪些二、各默认成员函数详解默认构造函数析构函数拷贝构造函数拷贝赋值运算符三、默认成员函数的注意事项总结一

conda安装GPU版pytorch默认却是cpu版本

《conda安装GPU版pytorch默认却是cpu版本》本文主要介绍了遇到Conda安装PyTorchGPU版本却默认安装CPU的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录一、问题描述二、网上解决方案罗列【此节为反面方案罗列!!!】三、发现的根本原因[独家]3.1 p

Kotlin Compose Button 实现长按监听并实现动画效果(完整代码)

《KotlinComposeButton实现长按监听并实现动画效果(完整代码)》想要实现长按按钮开始录音,松开发送的功能,因此为了实现这些功能就需要自己写一个Button来解决问题,下面小编给大... 目录Button 实现原理1. Surface 的作用(关键)2. InteractionSource3.

Android NDK版本迭代与FFmpeg交叉编译完全指南

《AndroidNDK版本迭代与FFmpeg交叉编译完全指南》在Android开发中,使用NDK进行原生代码开发是一项常见需求,特别是当我们需要集成FFmpeg这样的多媒体处理库时,本文将深入分析A... 目录一、android NDK版本迭代分界线二、FFmpeg交叉编译关键注意事项三、完整编译脚本示例四