如何在android中将Button和Text设置在Button的中心

时间:2017-02-21 16:03:08

标签: 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>

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

如果您想要裁剪图像并使文字保持居中,您只需将图像设置为按钮的背景:android:background="@drawable/YOUR_IMAGE"

这就是你的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:text="Order Station"
            android:textStyle="bold"
            android:background="@drawable/list" />

        <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:text="Quick Order"
            android:textStyle="bold"
            android:background="@drawable/power" />


        <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:text="Take Away"
            android:textStyle="bold"
            android:background="@drawable/coffeecup"/>

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