简体   繁体   中英

how to align the drawable to left of text in textview?

I want to display a drawable and text in a textview. When I use below code it is displaying both but drawable is in left most side of textview.

       <app.com.myapp.views.TextViewMedium
        android:id="@+id/approve_btn"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:background="@color/color_5eba6f"
        android:gravity="center"
        android:drawableLeft="@drawable/approve"
        android:text="@string/notification_approve"
        android:textColor="@color/color_FFFFFF"
        android:textSize="@dimen/dp_14" />

My text is aligned in center. I want to align drawable to left of that text.Gap between drawable and text should be just 6 dp. Is it possible to do? Or do I need to create a layout with imageview and textview to achieve the same?

Use these tags to set the image in your textView.

android:drawableLeft="@drawable/your_image"
android:drawablePadding="6dp"

Hope this helps!

This can be possible way :

  <TextView
            android:id="@+id/etName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_weight="1"
            android:background="@drawable/edittext"
            android:drawableLeft="@mipmap/icon_username"
            android:drawablePadding="6dp"
            android:gravity="fill_horizontal|fill|left"               
            android:padding="5dp"
            android:singleLine="true"
            android:textSize="15dp"       
            android:windowSoftInputMode="stateAlwaysHidden" />

Try this

<TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Sample Text"
                android:id="@+id/handling"
                android:textSize="20dp"
                android:drawableLeft="@drawable/emoticons_bear"
                android:drawablePadding="6dp"/>

You need to use drawableLeft attribute as below:

        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:id="@+id/userFname"
            android:drawableLeft="@drawable/user_name"
            android:drawablePadding="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp"
            android:textSize="16sp"
            android:textColor="@android:color/white"
            />

This is possible way to do it

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Your Text"
            android:id="@+id/yourTextView"
            android:textSize="12sp"
            android:drawableLeft="@drawable/yourDrawable"
            android:drawablePadding="6dp"/>

If not satisfies your demands you must use separate ImageView and TextView i think.. Hope to help you!

to do that you must use relative layout like this:

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

        <!--Your text view-->
        <TextView
            android:id="@+id/myText"/>

        <!--Your image view-->
        <ImageView
            android:layout_toLeftOf="@+id/myText"
            android:padding_right="6dp"/>
</RlativeLayout>

with that code you ImageView is forced to be at the left of your TextView and by 6dp spacing Don't forget you must use relativeLayout as a parent layout so you can use toLeftOf

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