簡體   English   中英

RelativeLayout和TextView沒有工作重力

[英]RelativeLayout and TextView with not working gravity

我有一個簡單的列表項布局來顯示圖像和標題。

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

        <ImageView
            android:id="@+id/image"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/image" />

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/image"
            android:layout_alignBottom="@+id/image"
            android:layout_toRightOf="@+id/image"
            android:gravity="center_vertical"
            android:text="Title" />
</RelativeLayout>

我的問題是文本引力應該是"center-vertical" 當我在開發人員設置中顯示視圖邊界時,我可以看到TextView大小比文本本身大,但重力保持在頂部。 問題出現在我的Android KitKat設備上,但不會出現在Android Froyo上。

這是一個已經修復的已知問題 ,它不會影響Marshmallow(盡管我還沒有嘗試過Lollipop)。 (不那么優雅)解決方案是將TextView包裝在FrameLayout

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

  <ImageView
    android:id="@+id/image"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/image" />

  <FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@id/image"
    android:layout_alignBottom="@id/image"
    android:layout_toRightOf="@id/image"
    android:layout_centerInParent="true">

    <TextView
      android:id="@+id/title"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_vertical"
      android:text="Title" />

  </FrameLayout>

</RelativeLayout>

android:gravity="center_vertical"更改為android:layout_centerInParent="true"

只需使用此布局,它就可以工作

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

            <ImageView
                android:id="@+id/image"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:src="@drawable/image" />

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_toRightOf="@+id/image"
                android:layout_centerInParent="true"
                android:text="Title" />
</RelativeLayout>
// Try this way,hope this will help you to solve your problem.

<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:gravity="center_vertical">

   <ImageView
    android:id="@+id/image"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:src="@drawable/ic_launcher" />

   <TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Title" />

</LinearLayout>

檢查此布局代碼和

圖片

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

            <ImageView
                android:id="@+id/image"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:src="@drawable/ic_launcher" />

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:layout_toRightOf="@+id/image"
                android:layout_centerInParent="true"
                android:text="@string/hello_world" />
            </RelativeLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM