【问题标题】:Padding not working in my Button填充在我的按钮中不起作用
【发布时间】:2018-12-29 04:32:24
【问题描述】:

在我的 Button 中,我有这个 XML:

 <Button
    android:id="@+id/btnFiltrarResultados"
    android:layout_width="18dp"
    android:layout_height="17dp"
    android:layout_above="@+id/searchView"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="-37dp"
    android:layout_marginEnd="29dp"
    android:background="@drawable/filtrar_explorar"
    android:cropToPadding="true"
    android:padding="20dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/txtExploreTitulo"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.01999998" />

但我似乎无法为其添加填充。我尝试过使用cropToPaddingandroid:src,但似乎没有任何帮助......

【问题讨论】:

标签: android xml android-layout


【解决方案1】:

这不是在按钮中添加图像的方法,就像您在 xml 文件中所做的那样android:background="@drawable/filtrar_explorar"。因为在默认情况下,背景图像会尽可能地缩放并忽略填充。所以好的做法是使用 ImageButtonandroid:src="@drawable/use_your_image" 并添加 android:scaletype="fitCenter"

<ImageButton
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:src="@drawable/use_your_image"
        android:scaleType="fitCenter"
        android:padding="10dp"
        />

【讨论】:

    【解决方案2】:

    你有

    android:layout_width="18dp"
    android:layout_height="17dp"
    

    期待padding 20dp

    【讨论】:

    • 这与问题有关吗?
    • 您需要负边距吗?
    • 我愿意,这是我可以将其设置在我想要的位置的唯一方法
    • 查看我编辑的答案,您的按钮太小,无法填充 20dp
    • 这个问题有一些关于填充与边距的漂亮图片,可能与此处相关stackoverflow.com/questions/21959050/… 填充在元素内部,所以如果你的高度为 17dp,顶部和底部的填充为 5dp你只剩下 7dp 的内容。
    猜你喜欢
    • 2017-08-17
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 2015-06-23
    • 2011-04-06
    • 2021-08-04
    • 2018-01-19
    相关资源
    最近更新 更多