垂直布局中心

我想做一个列表行布局。这个布局在最左边有一个图像视图,在图像视图旁边有一个文本视图,在最右边有一个图像视图。我希望它们都是中心垂直的。

<RelativeLayout
android:layout_width="fill_parent" android:layout_height="100dp"
android:gravity="center_vertical"
>
<ImageView
android:id="@+id/icon"
android:layout_width="50dp" android:layout_height="50dp"
android:layout_gravity="center_vertical" />
<TextView
android:id="@+id/func_text" android:layout_toRightOf="@id/icon"
android:layout_width="wrap_content" android:layout_height="100dp"
android:layout_gravity="center_vertical" />
<ImageView
android:layout_width="50dp" android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_gravity="center_vertical"
android:src="@drawable/arrow" />
</RelativeLayout>

我还尝试将 android:layout_centerVertical="true"添加到 textview,但结果是 textview 底部与两个图像视图对齐。 我在安卓4.2模拟器中试过这个。 有人能帮我吗?

109156 次浏览

use

 android:layout_centerVertical="true"

This maybe because the textview is too high. Change android:layout_height of the textview to wrap_content or use

android:gravity="center_vertical"

I have edited your layout. Check this code now.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#33B5E5"
android:padding="5dp" >


<ImageView
android:id="@+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:background="@android:drawable/ic_lock_lock" />


<TextView
android:id="@+id/func_text"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_gravity="center_vertical"
android:layout_toRightOf="@+id/icon"
android:gravity="center"
android:padding="5dp"
android:text="This is my test string............"
android:textColor="#FFFFFF" />


<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_gravity="center_vertical"
android:src="@android:drawable/ic_media_next" />


</RelativeLayout>

This is working for me.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rell_main_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#096d74" >


<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="@drawable/img_logo_large"
android:contentDescription="@null" />


</RelativeLayout>

Try aligning top and bottom of text view to one of the icon, this will make text view sharing same height as them, then set gravity to center_vertical to make the text inside text view center vertically.

<TextView
android:id="@+id/func_text" android:layout_toRightOf="@id/icon"
android:layout_alignTop="@id/icon" android:layout_alignBottom="@id/icon"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:gravity="center_vertical" />

For me, I had to remove

<item name="android:gravity">center_vertical</item>

from RelativeLayout, so children's configuration would work:

<item name="android:layout_centerVertical">true</item>

If View's height/width = wrap_content

use:

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

If View's height/width = match_parent

use:

android:gravity="center_vertical|center_horizontal"

Adding both android:layout_centerInParent and android:layout_centerVertical work for me to center ImageView both vertical and horizontal:

<ImageView
..
android:layout_centerInParent="true"
android:layout_centerVertical="true"
/>
       <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">


<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:layout_gravity="center_vertical"
android:layout_marginTop="@dimen/main_spacing_extra_big"
android:orientation="horizontal">


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"


android:fontFamily="@font/noto_kufi_regular"
android:text="@string/renew_license_municipality"
android:textColor="@color/sixth_text"
android:textSize="@dimen/main_text" />


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/noto_kufi_regular"
android:text="@{RenewLicenseBasicInfoFragmentVM.tvMunicipality}"
android:textColor="@color/sixth_text"
android:textSize="@dimen/main_text" />
</LinearLayout>


<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerInParent="true"
android:layout_gravity="center_vertical"
android:layout_marginTop="@dimen/main_spacing_extra_big"
android:orientation="horizontal">


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:fontFamily="@font/noto_kufi_regular"
android:text="@string/renew_license_license_number"
android:textColor="@color/sixth_text"
android:textSize="@dimen/main_text" />


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:fontFamily="@font/noto_kufi_regular"
android:text="@{RenewLicenseBasicInfoFragmentVM.tvLicenseNum}"
android:textColor="@color/sixth_text"
android:textSize="@dimen/main_text" />
</LinearLayout>`enter code here`


</RelativeLayout>
        <RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginHorizontal="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/default_button">


<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:layout_marginStart="15dp"
android:src="@drawable/google" />


<Button
android:id="@+id/btnGmailLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:paddingHorizontal="15dp"
android:text="@string/gmail_login_button_text"
android:textAllCaps="false"
android:textColor="@color/black" />
</RelativeLayout>