简体   繁体   中英

How can TextView be aligned right next to an ImageButton in LinearLayout

I am trying to have a TextView right next to an ImageButton . However, the TextView isn't aligned well. I want it to start at the same top position as the ImageButton .

This is how it looks right now:

在此处输入图片说明

Here is my layout:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:orientation="horizontal">

    <ImageButton
        android:id="@+id/myimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:src="@drawable/myimage"/>

    <TextView
        android:id="@+id/mytexview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14dp"
        android:text="My Text"
        android:textStyle="bold"
        android:layout_marginLeft="5dp"
        android:textColor="#DF533B"
        />

</LinearLayout>

set this property to view element android:gravity="top" inside the TextView tag.

I hope this will work.

Try this..

Add android:layout_gravity="center" for both ImageButton and TextView

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="15dp"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/myimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#FFFFFF"
        android:src="@drawable/myimage" />

    <TextView
        android:id="@+id/mytexview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="5dp"
        android:text="My Text"
        android:textColor="#DF533B"
        android:textSize="14dp"
        android:textStyle="bold" />
</LinearLayout>

Or Use android:drawableLeft="@drawable/ic_launcher" and add android:gravity="center" for that TextView

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="15dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/mytexview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="5dp"
        android:drawableLeft="@drawable/ic_launcher"
        android:gravity="center"
        android:text="My Text"
        android:textColor="#DF533B"
        android:textSize="14dp"
        android:textStyle="bold" />
</LinearLayout>

Try that:

<TextView
        android:id="@+id/mytexview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14dp"
        android:drawableLeft="@drawable/myimage"
        android:text="My Text"
        android:textStyle="bold"
        android:gravity="center_vertical"
        android:layout_marginLeft="5dp"
        android:textColor="#DF533B"/>

You have to do android:layout_height="match_parent" and set gravity center .

use below code:-

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="15dp"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/myimage"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:src="@drawable/myimage" />

    <TextView
        android:id="@+id/mytexview"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:text="My Text"
        android:gravity="center"
        android:textColor="#DF533B"
        android:textSize="14dp"
        android:textStyle="bold" />
</LinearLayout>

try,

android:gravity="top" in textview

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