【问题标题】:Button with background image text positioning带背景图片文字定位的按钮
【发布时间】:2017-09-26 15:31:40
【问题描述】:

我想将文本放置在我的按钮中,如下所示:

此时文本位于按钮的左下角:

我有四行这种类型的按钮,每行有两个按钮。这是我的一行按钮的代码:

         <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/main_fashion_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/thumbnail_channel_fashion"
                android:text="Fashion"
                android:textColor="#FFFFFF"
                android:gravity="bottom"
                android:layout_weight="1"/>


            <Button
                android:id="@+id/main_science_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/thumbnail_channel_science"
                android:text="Science"
                android:textColor="#FFFFFF"
                android:gravity="bottom"
                android:layout_weight="1"/>


        </LinearLayout>

如何在不移动背景图像的情况下在文本的左侧和下方添加一些填充?边距似乎在按钮周围添加了空白,而填充在背景图像和按钮边缘之间创建了空间。

感谢您的帮助!

【问题讨论】:

  • 你可以使用图片按钮
  • 只需为按钮添加填充/边距。
  • 文本不显示在 imagebutton://
  • Margin 只是在按钮周围创建空白区域,而 padding 在背景图像和按钮边缘之间创建空白区域

标签: android xml button


【解决方案1】:

你可以用这个

   <TextView
    android:id="@+id/main_fashion_button"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:background="@mipmap/ic_launcher"
    android:text="Fashion"
    android:textColor="#FFFFFF"
    android:gravity="bottom"
    android:padding="10dp">

【讨论】:

  • 这只是在背景图像和顶部边缘之间添加了一些空白://
  • 是的,我可以这样做,但不想在我的代码中添加额外的级别
  • 这是唯一的选择吗?
  • @Leero11 你可以用这个。
  • 在我的代码中不起作用://我每行都有两个这种类型的框,因此可能无法正常工作。因为它,我有重力 1
【解决方案2】:

看看这个

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:src="@drawable/image" />

    <TextView
        android:id="@+id/imageViewText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|left"
        android:layout_alignBottom="@+id/imageButton"
        android:text="FASHION"
        android:padding="16dp"
        android:textColor="#fff" />

</RelativeLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    • 2015-07-11
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 2012-05-12
    相关资源
    最近更新 更多