简体   繁体   中英

TextView and ImageView do not top-align at the same level

I have a very simply horizontal LinearLayout with one ImageView and one TextView and both layout_gravity is set to top . Both views do indeed align at the top of the layout, but the TextView is always slightly lower, and to get to align with the ImageView I have to give it a negative layout_marginTop .

Am I missing something here?

Layout

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

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.67"
        android:layout_gravity="top"
        android:layout_margin="16dp"
        android:textSize="16sp"
        android:text="This is some text\nwith several\nline breaks"/>

    <ImageView
        android:id="@+id/card_image"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.33"
        android:layout_gravity="top"
        android:layout_margin="16dp"
        android:src="@drawable/placeholder"/>
</LinearLayout>

Result 在设计器中工作正常,但在运行时看起来不对

代替android:layout_height="wrap_content"进行textview尝试使用android:layout_height =“ match_parent”并将其gravity设置为top

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM