在相对布局中,如何将文本视图放在图像按钮下方并将其与图像按钮的中心对齐?

In a relative layout, how to place a textview below an imagebutton and align it to the center of the imagebutton?

在相对布局中,我有一个大图像按钮,我必须在图像按钮下方放置一个文本视图。在 textview 声明中,我使用了 android:layout_alignLeft="@+id/Button1。但是因为按钮比textview大,所以布局如下:

______Imagebutton______

Textview_______________

我希望布局为:

____ImageButton____

_____TextView______

我该怎么做?

假设 ImageButton 没有填充 RelativeLayout 的宽度,最简单的方法可能是将以下内容添加到 TextView:

android:layout_below="@id/Button1"
android:layout_alignLeft="@id/Button1"
android:layout_alignRight="@id/Button1"
android:gravity="center"

这将使文本视图的宽度与 ImageButton 相同,文本居中(这可能符合您的需要,也可能不符合您的需要)。

如果 ImageButton 占据了 RelativeLayout 的整个宽度,这是一个更简单的情况——您可以简单地在 TextView 中添加以下内容:

android:layout_below="@id/Button1"
android:layout_centerInParent="true"

如果你想把任何东西放在RelativeLayout的中心,你可以使用这些属性

将 id 给 ImageButton,例如。 @+id/img_btn

现在 TextView,添加此属性:

android:layout_below="@+id/img_btn"
android:layout_centerInParent="true"