Keep text in TextView with drawableLeft centered

后端 未结 21 1155
野的像风
野的像风 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:49
    <TextView
        android:id="@+id/distance"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:drawableLeft="@drawable/distance"
        android:drawablePadding="10dp"
        android:padding="10dp"
        android:textAlignment="center"
        android:textColor="#ffffff"
        android:textSize="20sp" />
    
    0 讨论(0)
  • 2021-02-03 17:50

    no test too much, another way

        class CenterDrawableToggleButton @JvmOverloads constructor(
            context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
        ) : AppCompatToggleButton(context, attrs, defStyleAttr) {
    
            private val centerMatrix = Matrix()
    
            override fun onDraw(canvas: Canvas?) {
                canvas?.save()?:return
                centerMatrix.reset()
                centerMatrix.postTranslate(width / 2F - (getDrawWidth() / 2), 0F)
                canvas.concat(centerMatrix)
                super.onDraw(canvas)
                canvas.restore()
            }
    
            private fun getDrawWidth(): Float {
                return paint.measureText(text.toString()) + compoundPaddingRight + compoundPaddingLeft
            }
        }
    
    0 讨论(0)
  • 2021-02-03 17:53

    You can set the parent of the TextView as a RelativeLayout whose width is match_parent.

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    <TextView
            android:id="@+id/edit_location_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@id/add_location_text_view"
            android:gravity="start|center_vertical"
            android:layout_centerHorizontal="true"
            android:drawableStart="@android:drawable/ic_menu_edit"
            android:text="Edit Location" />
    
    </RelativeLayout>
    

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

    u can set your header like this

    <RelativeLayout
                 android:id="@+id/linearLayout1"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:background="@drawable/head" >
    
                 <ImageView
                     android:id="@+id/cat"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
    
                     android:layout_marginLeft="5dp"
                     android:onClick="onClick"
                     android:layout_marginTop="10dp"
                     android:src="@drawable/btn_back_" />
    
    
             <RelativeLayout
                 android:id="@+id/linearLayout1"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:gravity="center" >
    
               <TextView
     android:id="@+id/TvTitle"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text=""
     android:layout_marginLeft="10dp"
     android:layout_marginRight="10dp"
     android:textColor="#000000" 
    
     android:textSize="20sp"/>
                 </RelativeLayout>
    
             </RelativeLayout>
    

    Just Give Hieght of Textview,image source as per your need

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

    Try following:

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:padding="10dp"
        android:textAlignment="center" />
    
    0 讨论(0)
  • 2021-02-03 17:54

    simple as this.. try it..

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