简体   繁体   中英

how to align a textview to the right of imageview

I'm a beginner in android development and this is my code (it's relative layout)

<ImageView
    android:id="@+id/logo1"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:src="@drawable/telephone1"
    android:layout_marginTop="10dp"
    android:layout_below="@id/text4"/>

<TextView
    android:id="@+id/text5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="00966-13-8420444"
    android:fontFamily="sans-serif"
    android:textSize="20sp"
    android:layout_toRightOf="@id/logo1"
    />

I thought android:layout_toRightOf="@id/logo1 is enough and the text was on the right of the image (aligned correctly) but it appears on the top of device (the place of image on the bottom but not on bottom edge), what line of code I should add?

sorry for English. thank you

If I understood your question correctly, you want your text outside the picture, but aligned to its center, try this:

<ImageView
    android:id="@+id/logo1"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:src="@drawable/telephone1"
    android:layout_marginTop="10dp"
    android:layout_below="@id/text4"/>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="center_vertical">

    <TextView
        android:id="@+id/text5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="00966-13-8420444"
        android:fontFamily="sans-serif"
        android:textSize="20sp"
        android:layout_toRightOf="@id/logo1"/>
</LinearLayout>

If I have understood your questions clearly, You want to display image and textview in a single row. Also, Your textview is right side of imageView. If it is so. Please copy/paste below. It should work

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

    <ImageView
        android:id="@+id/logo1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:id="@+id/text5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:fontFamily="sans-serif"
        android:text="00966-13-8420444"
        android:textSize="20sp"
        />
</LinearLayout>

You can make a Layout Structure like this and then Move around the LinearLayout wherever you like using android:gravity

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="Horizontal">

<ImageView
    android:id="@+id/logo1"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:src="@drawable/telephone1"
    android:layout_marginTop="10dp"/>

<TextView
    android:id="@+id/text5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="00966-13-8420444"
    android:fontFamily="sans-serif"
    android:textSize="20sp"
    />

</LinearLayout>

try this

 <ImageView
        android:id="@+id/logo1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/telephone1"
        android:layout_marginTop="10dp"
        android:layout_below="@id/text4"
        android:layout_alignParentLeft="true"/>

    <TextView
        android:id="@+id/text5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="00966-13-8420444"
        android:fontFamily="sans-serif"
        android:textSize="20sp"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@id/logo1"

/>

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