【问题标题】:Android Buttons have an undesired marginAndroid Buttons 有多余的边距
【发布时间】:2017-02-23 22:22:18
【问题描述】:

我编写了一个测试应用程序来更好地描述我的问题。我通过这个活动开始了一个新的 Android 项目:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.jannevers.myapplication.MainActivity">

<Button
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</RelativeLayout>

现在第一个问题是,按钮并没有真正填充父级,它有一个边距(见截图):

我可以解决这个问题,方法是让按钮变大。

但是第二个问题是,大多数设备上的边距是相同的,但不是全部。例如。在三星 Galaxy S6 Edge 上,几乎没有余量(见截图):

这真的很奇怪,有人知道解决办法吗?

【问题讨论】:

  • 你可以尝试在你的相关布局中覆盖android:margin="0"
  • 不会改变任何东西
  • 布局是在fragment还是activity里面?
  • 它不是边距,也不是按钮内的填充,您可以轻松复制问题,并且显然按钮满足布局但按钮图像不满足按钮视图,有些人可能会说这是因为按钮的内置阴影或其他原因,但是您可以为按钮使用自定义的 9patch 背景,它将满足视图。
  • 是的,按钮符合布局,边距不是真正的正确词。嗯,我想使用带有按下状态的标准背景,但也许使用自定义背景是唯一的方法。

标签: android android-layout button margin


【解决方案1】:

使用style="?android:attr/borderlessButtonStyle"

<Button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    style="?android:attr/borderlessButtonStyle"/>

【讨论】:

  • 嗯不,我不想要无边框按钮。按钮应有边框,但没有“边距”
  • @SJ.Doe 空间不是边距。它的边界阴影。您可以创建按钮的边框。
  • 不明白你的意思。我知道这不是边距,它是按钮背景中的透明区域。问题是,在某些设备上它是不同的,这真的很糟糕,因为可见按钮的大小会改变。
【解决方案2】:

检查你的 style.xml 中是否有这样的布局边距规则:

<item name="android:layout_margin">10dp</item>

【讨论】:

  • 不,这是一个新的 android studio 项目。 styles.xml 仅包含带有 colorPrimary、colorPrimaryDark、colorAccent 的 AppTheme
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-06
  • 1970-01-01
  • 1970-01-01
  • 2013-02-07
  • 1970-01-01
  • 2015-02-18
  • 1970-01-01
相关资源
最近更新 更多