Android 如何将textview与图像的中心底部对齐

Android 如何将textview与图像的中心底部对齐,android,android-studio,Android,Android Studio,如何将textview与图像中心对齐?请参阅照片以便于理解: 我希望textview“Blade Runner 2049”与Blade Runner图像的中心对齐 我试过重心和填充,但都不起作用。。。非常感谢您的帮助。多谢各位 编辑:这是这个问题的旧xml,以防谷歌把任何人带到这里: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/

如何将
textview
与图像中心对齐?请参阅照片以便于理解:

我希望
textview
“Blade Runner 2049”与Blade Runner图像的中心对齐

我试过重心和填充,但都不起作用。。。非常感谢您的帮助。多谢各位

编辑:这是这个问题的旧xml,以防谷歌把任何人带到这里:

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

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:itemBackground="@color/colorWhite"
        app:itemIconTint="@drawable/selector"
        app:itemTextColor="@drawable/selector"
        app:menu="@menu/bottom_navigation_main" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="380dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:ems="10"
        android:hint="Search"
        android:inputType="text" />

    <ImageButton
        android:id="@+id/imgBtnFrozen"
        android:layout_width="180dp"
        android:layout_height="210dp"
        android:layout_alignEnd="@+id/editText"
        android:layout_alignTop="@+id/imgBtnKin"
        android:background="@drawable/frozen_poster"
        android:scaleType="centerCrop" />

    <ImageButton
        android:id="@+id/imgBtnHotelT"
        android:layout_width="180dp"
        android:layout_height="210dp"
        android:layout_alignEnd="@+id/editText"
        android:layout_alignTop="@+id/imgBtnBladeRunner"
        android:background="@drawable/hotel_transylvania_3_poster"
        android:scaleType="centerCrop" />

    <ImageButton
        android:id="@+id/imgBtnBladeRunner"
        android:layout_width="180dp"
        android:layout_height="210dp"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/editText"
        android:layout_marginTop="70dp"
        android:background="@drawable/blade_runner_poster"
        android:scaleType="centerCrop" />

    <ImageButton
        android:id="@+id/imgBtnKin"
        android:layout_width="180dp"
        android:layout_height="210dp"
        android:layout_alignParentBottom="true"
        android:layout_alignStart="@+id/editText"
        android:layout_marginBottom="80dp"
        android:background="@drawable/kin_poster"
        android:scaleType="centerCrop" />

    <TextView
        android:id="@+id/txtBladeRunner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/imgBtnBladeRunner"
        android:text="Blade Runner 2049 "
        android:textColor="@color/colorBlack" />

</RelativeLayout>

我想它可以帮助您我没有看到您的布局XML:

 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"

您应该使用RecyclerView/GridView来获得该布局。对于RecyclerView/GridView中的每个项目,请尝试以下XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:src="@drawable/poster" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Dunkirk"
        android:textColor="@android:color/black" />

</LinearLayout>

这将导致此布局(对于RecyclerView/GridView中的每个项目):

此外,如果有长标题,您可以使用:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <!-- find a good ratio for the poster -->
    <ImageView
        android:layout_gravity="center"
        android:layout_width="229dp"
        android:layout_height="340dp"
        android:src="@drawable/poster" />

    <!-- adjust maxWidth according to your poster ratio -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:maxWidth="200dp"
        android:text="Very long title that will need to be split."
        android:textColor="@android:color/black" />

</LinearLayout>

这将导致:

额外:浏览GridView的教程。或者用于RecyclerView。

试试这个

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/thumbImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="@drawable/ic_user_icon" /> // your image

    <TextView
        android:id="@+id/movieName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/thumbImage"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="8dp"
        android:text="Blade Runner 2049" />
</RelativeLayout>

//你的形象

要实现您的输出,您可以使用RelativeLayout。 以下是适用于您的案例的示例代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
    android:id="@+id/image"
    android:layout_margin="8dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/tsp"/>
<TextView
    android:layout_below="@id/image"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_alignEnd="@id/image"
    android:gravity="center_horizontal"
    android:text="Blade Runner 2049" />

</RelativeLayout>

在gridview中设置此选项

 <LinearLayout
            android:id="@+id/linearbot1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center">

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <ImageView
                    android:id="@+id/imgvw_mon1"
                    android:layout_width="29dp"
                    android:layout_height="26dp"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:layout_gravity="center"
                    app:srcCompat="@drawable/icon_mon1" />

                <TextView
                    android:id="@+id/tv_mon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/imgvw_mon1"
                    android:layout_centerHorizontal="true"
                    android:text="sample"
                    android:textColor="#e6e5e5"
                    android:textSize="10sp" />
            </RelativeLayout>

  </LinearLayout>


PS:您也可以使用本教程

对于那些想知道如何解决这个问题的人,我使用了@Krishna Sharma的答案和@Neeraj vishwakarma的答案,但通过添加

android:layout_alignStart="@id/imgBtnFrozen"
对于
TextView
,这是因为如果我不这样做,则
TextView
将只与屏幕的水平中心对齐(尽管它仍然位于
ImageButton
下方)


感谢大家的回答,您的答案也很棒

如果您也添加了布局的XML,那就太好了。您应该使用RecyclerView或GridView并构建一个包含图像和文本的项目来实现这一点。试着用谷歌搜索一个例子。请分享你的XML布局设计,否则回答这个问题将很困难。你可以查看答案,这应该行得通。这不应该行得通。TextView不可见,因为ImageView重载了它。谢谢,@grrigore我对代码做了更改。现在Textview将可见。是的,它将可见,但FrameLayout中的子项像堆栈一样工作,因此Textview现在将位于ImageView之上,但将
layout\u gravity
添加到Textview将使其在其父项(此处为FrameLayout)中居中,因此您将无法获得OP想要的布局。您将获得图像(比方说作为背景),其中有居中的文本。FrameLayout仅用于单个子级,或者仅当您希望子级重叠时才用于多个子级。读一读。这就是为什么你应该使用LinearLayout/RelativeLayout/ConstraintLayout的原因。我又读了一遍这个问题,我发现他把它表述得非常错误,但还是照着图去做。所以你的答案是好的,但不是他真正想要的。是的,你是对的,这就是为什么我把图像也放在其父图像的中心,并且这个布局可以在适配器中使用以实现输出。这个答案很好,但不是我想要的,谢谢你的回答though@wolnavi你可以根据这个答案调整你需要的。正如我所见,你有4个图像按钮,所以你可以像我一样包装每个按钮,如果它适合你的需要。