Keep text in TextView with drawableLeft centered

后端 未结 21 1154
野的像风
野的像风 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:43

    drawable is part of the TextView, so the textview's height will be the drawable's height if your text's height is smaller than drawable's height.

    You can set TextView's gravity attribute to center_vertical, so the text will be in the center.

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

    That work for me.

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/trusted"
            android:gravity="center"
            android:text="@string/trusted"/>
    
    </LinearLayout>
    
    0 讨论(0)
  • 2021-02-03 17:46

    Remove the DrawableLeft and use a container FrameLayout.

    <FrameLayout
         android:id="@+id/container"
         android:layout_width="match_parent"
         android:layout_height="wrap_content" >
    
         <ImageView
             android:id="@+id/image"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_marginLeft="5dp"
             android:layout_gravity="center_vertical"
             android:src="@drawable/image" />
    
         <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Text"
            android:layout_gravity="center" />
    </FrameLayout>
    
    0 讨论(0)
  • If none of the above solutions did not work for you?. Then try below answer

    Sample screen shot of the design.

    For the above image please find the code below.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.v7.widget.CardView
            android:id="@+id/my_card"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <LinearLayout
                android:id="@+id/bb"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:divider="@color/gray"
                android:orientation="horizontal"
                android:weightSum="2">
    
                <TextView
                    android:id="@+id/filter_tv"
                    style="?android:attr/buttonBarButtonStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="?attr/actionBarSize"
                    android:layout_gravity="center_horizontal"
                    android:layout_weight="1"
                    android:drawableLeft="@drawable/ic_baseline_filter_list"
                    android:gravity="center|fill_horizontal"
                    android:paddingLeft="60dp"
                    android:text="Filter"
                    android:textAllCaps="false"
                    android:textStyle="normal" />
    
                <View
                    android:layout_width="1dp"
                    android:layout_height="match_parent"
                    android:layout_below="@+id/bb"
                    android:background="@color/gray" />
    
                <TextView
                    android:id="@+id/sort_tv"
                    style="?android:attr/buttonBarButtonStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="?attr/actionBarSize"
                    android:layout_gravity="center|center_horizontal"
                    android:layout_weight="1"
                    android:drawableLeft="@drawable/ic_baseline_sort"
                    android:drawablePadding="20dp"
                    android:gravity="center|fill_horizontal"
                    android:paddingLeft="40dp"
                    android:text="Sort"
                    android:textAllCaps="false" />
    
            </LinearLayout>
        </android.support.v7.widget.CardView>
    
        <View
            android:layout_width="match_parent"
            android:layout_height="0.9dp"
            android:layout_below="@+id/my_card"
            android:background="@color/gray" />
    </RelativeLayout>
    
    0 讨论(0)
  • 2021-02-03 17:48

    I had same problem and I solved it with small changes in Textview,

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:drawableLeft="@drawable/search_red"
        android:text="your text goes here"
        />
    
    0 讨论(0)
  • 2021-02-03 17:48

    You can use android:gravity="center_vertical" to center the text vertically with the image. Or it can be centered inside the textView using android:gravity="center".

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:drawableLeft="@drawable/icon"
        android:text="Your text here" />

    0 讨论(0)
提交回复
热议问题