线性布局中的中心图像视图和文本视图

Ale*_*lex -3 android textview imageview android-layout android-linearlayout

我想要搜索图标下方的搜索文本。搜索图标+文本组应位于屏幕中央。

想要什么:____________屏幕实际是什么样子:

在此输入图像描述 在此输入图像描述

注意:我使用线性布局,因为我需要直接在下面的列表视图。它将用于搜索栏。对于那些建议使用 TextView 的人android:drawableTop,我尝试过,但图像太小(即使我将图像设置为 500px),而在图像视图中将图像设置为“100dpx100dp”时也是如此。

代码

<LinearLayout 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:orientation="vertical">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center_horizontal"
            app:srcCompat="@drawable/ic_search_gray_500px" />

        <TextView
            android:id="@+id/searchTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/search_hint"
            android:layout_gravity="center_horizontal"
            tools:layout_editor_absoluteX="130dp"
            tools:text="@string/search_hint" />

    </android.support.constraint.ConstraintLayout>

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

参考

小智 5

下面的代码将解决您的问题:

   <RelativeLayout 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="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/app_logo_main" />

            <TextView
                android:id="@+id/searchTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Search"
                android:textAlignment="center"
                android:textColor="@color/colorPrimary" />

        </LinearLayout>

    </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)