我正在尝试居中的图像(加号)在一个按钮,这也有一个文本下面。但是我想不出怎么解决这个问题。
代码如下:
<?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>欢迎您的帮助,谢谢!
发布于 2018-01-04 04:58:52
基本上,我建议你实现一个框架布局,因为你想在按钮上显示一个图像和文本信息。在框架布局中,包括ImageView (在本例中是加号)和文本视图(按钮的标题或您希望调用它的任何名称)。
XML文件
<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类
RelativeLayout layout = findViewById(R.id.layout);
layout.bringToFront();请注意,我没有加号的图像,所以我使用了一个已经在可绘制文件夹中的图像。

让我知道这是否有效,或者根据您的问题,我是否有能力脱离网格;)
https://stackoverflow.com/questions/48084116
复制相似问题