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使用java实现网络连通性检查详解

《Android使用java实现网络连通性检查详解》这篇文章主要为大家详细介绍了Android使用java实现网络连通性检查的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录NetCheck.Java(可直接拷贝)使用示例(Activity/Fragment 内)权限要求

2025最新版Android Studio安装及组件配置教程(SDK、JDK、Gradle)

《2025最新版AndroidStudio安装及组件配置教程(SDK、JDK、Gradle)》:本文主要介绍2025最新版AndroidStudio安装及组件配置(SDK、JDK、Gradle... 目录原生 android 简介Android Studio必备组件一、Android Studio安装二、A

Android实现图片浏览功能的示例详解(附带源码)

《Android实现图片浏览功能的示例详解(附带源码)》在许多应用中,都需要展示图片并支持用户进行浏览,本文主要为大家介绍了如何通过Android实现图片浏览功能,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

在Android中使用WebView在线查看PDF文件的方法示例

《在Android中使用WebView在线查看PDF文件的方法示例》在Android应用开发中,有时我们需要在客户端展示PDF文件,以便用户可以阅读或交互,:本文主要介绍在Android中使用We... 目录简介:1. WebView组件介绍2. 在androidManifest.XML中添加Interne

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

Nginx中配置使用非默认80端口进行服务的完整指南

《Nginx中配置使用非默认80端口进行服务的完整指南》在实际生产环境中,我们经常需要将Nginx配置在其他端口上运行,本文将详细介绍如何在Nginx中配置使用非默认端口进行服务,希望对大家有所帮助... 目录一、为什么需要使用非默认端口二、配置Nginx使用非默认端口的基本方法2.1 修改listen指令

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

更改linux系统的默认Python版本方式

《更改linux系统的默认Python版本方式》通过删除原Python软链接并创建指向python3.6的新链接,可切换系统默认Python版本,需注意版本冲突、环境混乱及维护问题,建议使用pyenv... 目录更改系统的默认python版本软链接软链接的特点创建软链接的命令使用场景注意事项总结更改系统的默

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Spring Boot 中的默认异常处理机制及执行流程

《SpringBoot中的默认异常处理机制及执行流程》SpringBoot内置BasicErrorController,自动处理异常并生成HTML/JSON响应,支持自定义错误路径、配置及扩展,如... 目录Spring Boot 异常处理机制详解默认错误页面功能自动异常转换机制错误属性配置选项默认错误处理