一.问题重述

摘要里描述的可能不太清楚,问题如下图:

如何消除Button1和Button2之间的空隙,以及Button与左右边界之间的空隙?

二.问题根源

这里出现的空隙其实是Button的背景图片中的透明部分,如下图:(两个按钮被同时按下)

因为间隙是Button自身的一部分,所以设置margin和padding为0也无法消除(至于把margin设置为负值,额,这算解决方案吗?)

三.解决方案及验证

  1. 设置Button的style属性:

    <Button style="?android:attr/buttonBarButtonStyle"/>
    
    (初始)
    (两个按钮都被按下)
    方案1的效果:可以消除间隙,并且不影响按钮默认的点击效果(背景变蓝)
  2. 设置Button的style为自定义style(或者直接设置Button的background属性为自定义图片):
    <style name="mybutton">
    <item name="android:background" >@drawable/whitecolor</item>
    <item name="android:padding">0dp</item>
    <item name="android:gravity">center</item>
    </style>
    (初始/按下)
    方案2的效果:可以消除间隙,但按钮默认的点击效果没了
  3. 设置Button的background为透明色:
    android:background="#00000000"
    
    (初始/按下)
    方案3的效果:可以消除间隙,但按钮默认的点击效果没了(和方案2效果一样)
  4. 把LinearLayout换为TableLayout:
    <TableLayout
    android:stretchColumns="*"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
    <TableRow>
    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_column="0"
    android:text=" Button1" />
    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_column="1"
    android:text=" Button2" />
    </TableRow>
    </TableLayout>
    方案4的效果:无法消除间隙
  5. 自定义selector作为Button背景:
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/highlight"
    android:state_pressed="true"></item>
    <item android:drawable="@drawable/normal"></item> </selector>
    android:background="@drawable/bg_btn"
    
    (左边按钮被按下)
    方案5的效果:可以消除间隙,并且不影响按钮的默认点击效果(和方案1的效果类似,比方案1更好一些,可以自定义按钮背景)

四.总结

消除按钮间隙的关键在于改background属性,用边框不含透明色的图片作为按钮的背景即可消除间隙

消除按钮间隙建议使用方案5:自定义selector作为按钮背景图片

Android开发消除横向排列的多个Button之间的空隙的更多相关文章

  1. Android开发8:UI组件TextView,EditText,Button

    版本:Android4.3 API18 学习整理:liuxinming TextView 概述 TextView直接继承了View(EditText.Button两个UI组件类的父类) TextVie ...

  2. Android开发-之五大布局

    在html中大家都知道布局是什么意思了,简单来说就是将页面划分模块,比如html中的div.table等.那么Android中也是这样的.Android五大布局让界面更加美化,开发起来也更加方便.当然 ...

  3. Android开发1:基本UI界面设计——布局和组件

    前言 啦啦啦~本学期要开始学习Android开发啦~ 博主在开始学习前是完完全全的小白,只有在平时完成老师要求的实验的过程中一步一步学习~从此篇博文起,博主将开始发布Android开发有关的博文,希望 ...

  4. Android开发自学笔记(Android Studio)—4.1布局组件

    一.引言 Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦.组件按照布局的要求依次排列,就组成了用户所看见的界面.在Android4.0之前,我们通常说 ...

  5. 【Android开发学习笔记】【第八课】五大布局-下

    概念 五大布局上一篇文章已经介绍了 LinearLayout RelativeLayout 这一篇我们介绍剩下的三种布局 FrameLayout 五种布局中最佳单的一种布局.在这个布局在整个界面被当成 ...

  6. Android开发 - 掌握ConstraintLayout(六)链条(Chains)

    本文我们介绍链条(Chains),使用它可以将多个View连接起来,互相约束. 可以创建横向的链条,也可以创建纵向的链条,我们以横向的链条举例: 我们先创建三个按钮: 我们选中三个按钮后在上面点右键创 ...

  7. android开发分辨率适配总结

    重要概念 什么是屏幕尺寸.屏幕分辨率.屏幕像素密度? 什么是dp.dip.dpi.sp.px?他们之间的关系是什么? 什么是mdpi.hdpi.xdpi.xxdpi?如何计算和区分? 在下面的内容中我 ...

  8. Android开发——LinearLayout和RelativeLayout的性能对比

    0. 前言 我们都知道新建一个Android项目自动生成的Xml布局文件的根节点默认是RelativeLayout,这不是IDE默认设置,而是由android-sdk\tools\templates\ ...

  9. Android开发之自定义的ListView(UITableViewController)

    Android开发中的ListView, 顾名方法思义,就是表视图.表示图在iOS开发中就是TableView.两者虽然名称不一样,但是其使用方法,使用场景以及该控件的功能都极为相似,都是用来展示大量 ...

随机推荐

  1. 个人总结-----非贪心算法的图的m着色判断及优化问题

    1.问题描述: 对于著名的图的m着色,有两个主要的问题,一个是图的m色判定问题,一个是图的m色优化问题,描述如下. 图的m色判定问题: 给定无向连通图G和m种颜色.用这些颜色为图G的各顶点着色.问是否 ...

  2. 让IIS支持PHP的配置步骤

    本文转自:http://marsren.blog.51cto.com/116511/41199/ 在Windows Server 2003的IIS6下配置ISAPI方式的PHP,配置方法是,在IIS的 ...

  3. chrome input去除黄色背景色

    input:-webkit-autofill { -webkit-box-shadow: 0 0 0px 1000px white inset; border: 1px solid #CCC!impo ...

  4. 对象导航查询和OID查询(补)

    ----------------siwuxie095                                 对象导航查询         以客户和联系人为例(一对多)     1.应用场景 ...

  5. django的数据库操作回顾

    补充一个filter中大于小于的用法 models.course_info.objects.filter(id__gte=30).delete() _exact 精确等于 like 'aaa' __i ...

  6. 84直方图最大矩形覆盖 · Largest Rectangle in Histogram

    [抄题]: Given n non-negative integers representing the histogram's bar height where the width of each ...

  7. iOS 基于MVC设计模式的基类设计

    iOS 基于MVC设计模式的基类设计 https://www.jianshu.com/p/3b580ffdae00

  8. 4.n的高精度阶乘---优化

    题目:对于每组测试数据,在一行中给出一非负整数n(n小于等于100) 样例输入 3 5 10 样例输出 6 120 3628800 超时的代码如下:#include <iostream># ...

  9. POJ 1122.FDNY to the Rescue! Dijkstra

    FDNY to the Rescue! Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2808   Accepted: 86 ...

  10. 查询yum包安装路径

    rpm -ql php71-php yum install json yum install libcurl