简体   繁体   English

具有背景图像文字定位的按钮

[英]Button with background image text positioning

I would like to position the text in my button like this: 我想这样在我的按钮中放置文本:

在此处输入图片说明

At the moment the text is positioned in the bottom left corner of the button: 此刻,文本位于按钮的左下角:

在此处输入图片说明

I have four rows of this type of buttons and in every row there is two buttons. 我有四排这种类型的按钮,每行有两个按钮。 Here is my code for one row of buttons: 这是我一行按钮的代码:

         <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>

how can I add some padding to the left and below of the text without moving the background image? 如何在不移动背景图像的情况下在文本的左侧和下方添加一些填充? Margin seems to add white space around button and padding creates space between background image and button's edges. 边距似乎在按钮周围添加了空白,并且填充在背景图像和按钮边缘之间创建了空间。

Thanks for help! 感谢帮助!

You can use this 你可以用这个

   <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">

Check this out 看一下这个

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM