Keep text in TextView with drawableLeft centered

后端 未结 21 1218
野的像风
野的像风 2021-02-03 17:10

In my app I have a header bar which consists of a single textview with fill_parent as width, which have a specific background color and some centered text. Now I want to add a d

相关标签:
21条回答
  • 2021-02-03 17:31

    use this way

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/White"
         >
      
      <RelativeLayout
           android:id="@+id/rlheader"
           android:layout_width="fill_parent"
           android:layout_height="50dp"
           android:layout_alignParentTop="true"
           android:background="@color/HeaderColor"
          
            >
     
            <TextView
               
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_centerHorizontal="true"
               android:layout_centerVertical="true"
               android:text="Header_Title"
                />
     
            <ImageView
               android:id="@+id/ivSetting"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignParentLeft="true"
               android:layout_centerVertical="true"
               android:layout_marginLeft="10dp"
               android:src="@drawable/setting" />
     
            
        </RelativeLayout>
    
    </RelativeLayout>
          
    

    it look something like this snap of my demo project

    Header Image

    0 讨论(0)
  • 2021-02-03 17:34

    Though fragile, you can avoid the use of a wrapper Layout by setting a negative padding on the drawable:

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:drawableLeft="@drawable/icon"
        android:drawablePadding="-20sp"
        android:text="blah blah blah" />
    

    You'll have to adjust the padding to the width of the drawable, but you're left with just a single TextView instead of an extra LinearLayout or etc.

    0 讨论(0)
  • 2021-02-03 17:34

    One simple solution is to use a transparent image with the same size on the opposite side of the textview. In my example i copied my actual vector_image and changed the colors to be transparent.

    <TextView
            android:id="@+id/name"
            style="@style/TextStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableEnd="@drawable/ic_vector_caret_down_green"
            android:drawableStart="@drawable/ic_vector_space_18"
            android:gravity="center"
            android:padding="12dp"
            android:textColor="@color/green"/>
    
    0 讨论(0)
  • 2021-02-03 17:41

    The accepted answer is not working for me, it fails if TextView width is match parent I did it using FrameLayout.

     <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="10dp"
            android:background="#000">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:drawableStart="@drawable/ic_circle_check_white"
                android:drawablePadding="10dp"
                android:text="Header"
                android:textColor="#fff"
                android:textSize="14sp"/>
    
        </FrameLayout>
    
    0 讨论(0)
  • the best way is to use ConstraintLayout, to make the textview at the center of the layout and the width warp_content(don't use 0dp now) so that the drawable will follow the text.

    <TextView
        android:id="@+id/tv_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test"
        android:drawableLeft="@drawable/you_drawable"
        android:drawablePadding="5dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        />
    
    0 讨论(0)
  • 2021-02-03 17:43

    I've faced a similar problem. Solved it by using single TextView with layout_width="wrap_content" and layout_gravity="center" parameters:

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/some_drawable"
        android:text="some text"
        android:layout_gravity="center"
        android:gravity="center"
        />
    
    0 讨论(0)
提交回复
热议问题