带有图标或图像的 ImageButton 中未显示文本

text not showing in ImageButton with icon or image

我需要一些帮助,我无法在 ImageButton 中的图标或图像下方或下方显示文本 我尝试使用 TextView 但它弄乱了我的按钮 我不知道如何显示文本。这是我的 xml.

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">


    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:orientation="horizontal"
            android:background="#eeeeee" android:layout_marginTop="30dp">

        <ImageButton
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="2dp"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:padding="30dp"
                android:scaleType="fitCenter"
                android:background="@drawable/button1_style"
                android:src="@drawable/icon1_48dp"
                style="@style/Widget.AppCompat.Button.Colored" />

       <ImageButton
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="2dp"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:padding="30dp"
                android:scaleType="fitCenter"
                style="@style/Widget.AppCompat.Button.Colored"
                android:background="@drawable/button2_style"
                android:src="@drawable/icon2_48dp"
                android:tint="#ffffff"

                />

    </LinearLayout>

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:orientation="horizontal"
            android:background="#eeeeee" android:layout_marginBottom="20dp">

    <ImageButton
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="2dp"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:padding="30dp"
                android:scaleType="fitCenter"
                style="@style/Widget.AppCompat.Button.Colored"
                android:src="@drawable/icon3_48dp"
                android:background="@drawable/button3_style"
                android:tint="#ffffff"
              />

        <ImageButton
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="2dp"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:padding="30dp"
                android:scaleType="fitCenter"
                style="@style/Widget.AppCompat.Button.Colored"
                android:src="@drawable/icon4_48dp"
                android:background="@drawable/button4_style"
               />
    </LinearLayout>

</LinearLayout>

我认为你可以使用 TextView 作为你的想法:

按钮 1 的示例:

<TextView
    android:layout_width="wrap_content"
    android:background="@drawable/button1_style"
    android:drawableTop="@drawable/icon1_48dp"
    android:text = "Button1"
    android:layout_height="wrap_content"/>

如有需要请添加paddingmargindrawablePadding

以下是 4 张图片和下方 4 条文字的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:orientation="horizontal"
        android:background="#eeeeee" android:layout_marginTop="30dp">

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="2dp"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:padding="30dp"
            android:scaleType="fitCenter"
            android:background="@drawable/button1_style"
            android:src="@drawable/icon1_48dp"
            style="@style/Widget.AppCompat.Button.Colored" />


        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="2dp"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:padding="30dp"
            android:scaleType="fitCenter"
            style="@style/Widget.AppCompat.Button.Colored"
            android:background="@drawable/button2_style"
            android:src="@drawable/icon2_48dp"
            android:tint="#ffffff"

            />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/TextViewlayout1"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:weightSum="2"
        android:layout_gravity="center"
        android:layout_height="wrap_content">
        <TextView
            android:text="Something 1"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:text="Something 2"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:orientation="horizontal"
        android:background="#eeeeee" >

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="2dp"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:padding="30dp"
            android:scaleType="fitCenter"
            style="@style/Widget.AppCompat.Button.Colored"
            android:src="@drawable/icon3_48dp"
            android:background="@drawable/button3_style"
            android:tint="#ffffff"
            />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="2dp"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:padding="30dp"
            android:scaleType="fitCenter"
            style="@style/Widget.AppCompat.Button.Colored"
            android:src="@drawable/icon4_48dp"
            android:background="@drawable/button4_style"
            />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/TextViewlayout2"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:weightSum="2"
        android:layout_gravity="center"
        android:layout_height="wrap_content">
        <TextView
            android:text="Something 3"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:text="Something 4"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>