线性布局中的居中图像视图和文本视图
Center image view and text view in linear layout
我想要搜索图标下方的搜索文本。搜索图标+文本组应在屏幕中央。
我想要的:____________What屏幕实际上看起来像:
注意:我使用了线性布局,因为我需要直接在下面的列表视图。它将用于搜索栏。对于那些建议使用 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>
Reference
您可以使用RelativeLayout
.
<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">
<RelativeLayout
android:id="@+id/rlROOT"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:gravity="center_vertical"
android:src="@drawable/ic_search_gray_500px" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
/>
</RelativeLayout>
<ListView
android:id="@android:id/list"
android:layout_below="@+id/rlROOT"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
只使用TextView,TextView中有一个属性drawableTop,用它来做图片。如果你想要图像和文本之间有一些间距,那么使用 drawablePadding。
<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">
<TextView
android:id="@+id/searchTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_search_gray_500px"
android:text="@string/search_hint"
android:layout_centerInParent="true />
<ListView
android:layout_below="searchTextView"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
试试这个 :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone">
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/searchTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/search_hint"
android:textAlignment="center"
android:textColor="@color/colorBlack" />
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
以下代码将解决您的问题:
<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>
我想要搜索图标下方的搜索文本。搜索图标+文本组应在屏幕中央。
我想要的:____________What屏幕实际上看起来像:
注意:我使用了线性布局,因为我需要直接在下面的列表视图。它将用于搜索栏。对于那些建议使用 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>
Reference
您可以使用RelativeLayout
.
<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">
<RelativeLayout
android:id="@+id/rlROOT"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:gravity="center_vertical"
android:src="@drawable/ic_search_gray_500px" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
/>
</RelativeLayout>
<ListView
android:id="@android:id/list"
android:layout_below="@+id/rlROOT"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
只使用TextView,TextView中有一个属性drawableTop,用它来做图片。如果你想要图像和文本之间有一些间距,那么使用 drawablePadding。
<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">
<TextView
android:id="@+id/searchTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_search_gray_500px"
android:text="@string/search_hint"
android:layout_centerInParent="true />
<ListView
android:layout_below="searchTextView"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
试试这个 :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone">
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/searchTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/search_hint"
android:textAlignment="center"
android:textColor="@color/colorBlack" />
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
以下代码将解决您的问题:
<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>