首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用TableLayout使按钮中的图像居中

如何使用TableLayout使按钮中的图像居中
EN

Stack Overflow用户
提问于 2018-01-04 03:44:14
回答 1查看 26关注 0票数 0

我正在尝试居中的图像(加号)在一个按钮,这也有一个文本下面。但是我想不出怎么解决这个问题。

代码如下:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<TableLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="add measure"
        android:drawableTop="@drawable/ic_add_white_24dp" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1" />
</TableRow>

</TableLayout>

欢迎您的帮助,谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-04 04:58:52

基本上,我建议你实现一个框架布局,因为你想在按钮上显示一个图像和文本信息。在框架布局中,包括ImageView (在本例中是加号)和文本视图(按钮的标题或您希望调用它的任何名称)。

XML文件

代码语言:javascript
复制
<TableLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/tableLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" >

            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1">

                <RelativeLayout
                    android:id="@+id/layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <ImageView
                        android:id="@+id/image"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher_background"
                        android:layout_centerVertical="true"
                        android:layout_centerHorizontal="true"
                        />

                    <TextView
                        android:id="@+id/textView"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="ADD MEASURE"
                        android:layout_below="@id/image"
                        android:layout_centerHorizontal="true"/>

                </RelativeLayout>

                <Button
                    android:id="@+id/button1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    />
            </FrameLayout>
            <Button
                android:id="@+id/button2"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1" />
        </TableRow>

    </TableLayout>

您还必须在主活动中包含以下语句,因为这些元素将始终隐藏在您的按钮后面。因此,您将需要连接您的布局,并在MainActivity中使用与我相同的函数。

MainActivity类

代码语言:javascript
复制
RelativeLayout layout = findViewById(R.id.layout);
layout.bringToFront();

请注意,我没有加号的图像,所以我使用了一个已经在可绘制文件夹中的图像。

让我知道这是否有效,或者根据您的问题,我是否有能力脱离网格;)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48084116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档