How can I align my ImageView with my TextView in a LinearLayout?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-08 19:14:15

问题


I have the following ImageView and TextView:

Here is the XML:

<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/headerLinearLay" android:orientation="horizontal">
        <ImageView android:src="@drawable/icon" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/avatarImageView"></ImageView>
        <TextView android:layout_height="wrap_content" android:id="@+id/usernameTextView" android:text="TextView" android:layout_width="wrap_content" android:paddingLeft="4px"></TextView>
    </LinearLayout>

How can I make the image and the text be positioned at the same height? I also want the ImageView to be in the corner


回答1:


Try this:

<RelativeLayout
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:id="@+id/headerLinearLay">
    <ImageView
        android:id="@+id/avatarImageView"
        android:src="@drawable/icon"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"></ImageView>
    <TextView
        android:id="@+id/usernameTextView"
        android:text="TextView"
        android:paddingLeft="4dp"
        android:layout_toRightOf="@id/avatarImageView"
        android:layout_centerVertical="true"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"></TextView>
</RelativeLayout>



回答2:


Here is a simple solution that works well. Essential here is to add: android:layout_gravity="center_vertical" to TextView

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/icon" />

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

    </LinearLayout>



回答3:


<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:layout_weight="0.04" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="204dp"
    android:layout_height="200dp"
    app:srcCompat="@mipmap/ic_launcher" />


来源:https://stackoverflow.com/questions/6115567/how-can-i-align-my-imageview-with-my-textview-in-a-linearlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!