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 12解决push framework.jar无法开机的方法小结

《Android12解决pushframework.jar无法开机的方法小结》:本文主要介绍在Android12中解决pushframework.jar无法开机的方法,包括编译指令、框架层和s... 目录1. android 编译指令1.1 framework层的编译指令1.2 替换framework.ja

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

CentOS7更改默认SSH端口与配置指南

《CentOS7更改默认SSH端口与配置指南》SSH是Linux服务器远程管理的核心工具,其默认监听端口为22,由于端口22众所周知,这也使得服务器容易受到自动化扫描和暴力破解攻击,本文将系统性地介绍... 目录引言为什么要更改 SSH 默认端口?步骤详解:如何更改 Centos 7 的 SSH 默认端口1

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32