java - 如何在android中将图像和文本设置在Button的中心

标签 java android

我创建了六个带有图像和文本的按钮......我只是想让这些图像和文本出现在按钮的中心,注意文本就在图像下方,这是我的 XML 代码......如果有人能解决这个问题,只需在楼下写下 XML 代码,抱歉我的英语不好,我希望这可以理解

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="3" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="2" >

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:drawableTop="@drawable/list"
                android:text="Order Station"
                android:textStyle="bold"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="2" />
        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="2" >

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:drawableTop="@drawable/power"
                android:background="#ff4545"
                android:text="Quick Order"
                android:textStyle="bold" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="4"
                />
        </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="2" >
        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:drawableTop="@drawable/coffeecup"
            android:text="Take Away"
            android:textStyle="bold"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="4" />
    </LinearLayout>
    </LinearLayout>
</FrameLayout>

最佳答案

您必须使用其他布局(线性或相对)并将drawableTop放置在TextView上,并通过提供onClickListener将该LinearLayout用作按钮。

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="3" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="2" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:layout_weight="1"> //give onclick to this layout to act as a button
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableTop="@drawable/list"
                android:gravity="center"
                android:text="Order Station"
                android:textStyle="bold"/>

        </LinearLayout>

        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="2" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="2" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:background="#ff4545"
            android:layout_weight="1">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableTop="@drawable/power"
                android:gravity="center"
                android:text="Quick Order"
                android:textStyle="bold"/>

        </LinearLayout>
        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="4"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="2" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:layout_weight="1">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableTop="@drawable/coffeecup"
                android:gravity="center"
                android:text="Take Away"
                android:textStyle="bold"/>

        </LinearLayout>
        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="4" />
    </LinearLayout>
</LinearLayout>

或者将按钮的高度设置为wrap_content。

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="2" >

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@drawable/list"
            android:text="Order Station"
            android:textStyle="bold"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="2" />
    </LinearLayout>

关于java - 如何在android中将图像和文本设置在Button的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42372231/

相关文章:

java - JsonPath SUPPRESS_EXCEPTIONS 似乎不起作用

java - 比较文件 .class Beyond Compare 4

java - 在文本字段中设置文本不起作用

android - Admob 横幅广告无法在 Android 上正常运行

android - Android通知时间格式是如何更新的?

JavaFX TabPane 排序选项卡造成严重破坏

java - 我可以检查正在填充的集合的大小吗

android - Android布局中的占位符文本(仅限设计过程)?

android - 在 RelativeLayout 中获取 subview

android - 如何控制启动 Intent 时显示的应用程序列表?