Android 将文本保留在 TextView 中,drawableLeft 居中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11261692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:33:55  来源:igfitidea点击:

Keep text in TextView with drawableLeft centered

androidtextview

提问by Jacob

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 drawable on the left side of the header bar, so I set the drawableLeft and sure enough the text and image is displayed. However the problem here is that the text is no longer properly centered, e.g., when the drawable is added the text is shifted a bit to the right as shown in the screenshots here:

在我的应用程序中,我有一个标题栏,它由单个 textview 组成,宽度为 fill_parent,它具有特定的背景颜色和一些居中的文本。现在我想在标题栏的左侧添加一个drawable,所以我设置了drawableLeft,果然显示了文本和图像。然而,这里的问题是文本不再正确居中,例如,当添加 drawable 时,文本会向右移动一点,如此处的屏幕截图所示:

without drawable
with drawable

没有可绘制的
可绘制

Is there anyway I can center the text properly and have the drawable positioned as it is above without using an additional layout item (such as a LinearLayout)?

无论如何我可以正确地将文本居中并将可绘制对象放置在上面而不使用额外的布局项(例如 LinearLayout)?

采纳答案by blahdiblah

Though fragile, you can avoid the use of a wrapper Layout by setting a negativepadding 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.

您必须将内边距调整为可绘制对象的宽度,但只剩下一个 TextView 而不是额外的 LinearLayout 等。

回答by Rohan Taneja

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

您可以将 TextView 的父级设置为宽度为 match_parent 的 RelativeLayout。

<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>

This is what the result looks like.

这就是结果的样子。

回答by birdy

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

我遇到过类似的问题。通过使用单个TextViewwith layout_width="wrap_content"layout_gravity="center"参数解决它:

<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"
    />

回答by Programmer

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" />

回答by Deepak

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

我遇到了同样的问题,我通过 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"
    />

回答by Hafiz Waleed Hussain

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>

回答by Khan

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

标题图片

回答by Quentin Colle

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".

您可以使用android:gravity="center_vertical"将文本与图像垂直居中。或者它可以使用 textView 居中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" />

回答by abcfrom0

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.

drawable 是 TextView 的一部分,因此如果您的文本高度小于 drawable 的高度,则 textview 的高度将是 drawable 的高度。

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

你可以将TextView的gravity属性设置为center_vertical,这样文本就会居中。

回答by vikas singh

> This line is important ->> android:gravity="start|center_vertical


 <LinearLayout
            android:layout_width="match_parent"
            android:layout_margin="@dimen/marginplus2"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:text="@string/like"
                android:gravity="start|center_vertical"
                android:drawablePadding="@dimen/marginplus1"
                android:drawableLeft="@drawable/ic_like"
                android:layout_height="wrap_content" />

            <TextView
                android:layout_width="0dp"
                android:layout_weight="1.5"
                android:text="@string/comments"
                android:drawablePadding="@dimen/marginplus1"
                android:gravity="start|center_vertical"
                android:drawableLeft="@drawable/ic_comment"
                android:layout_height="wrap_content" />
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:text="@string/share"
                android:drawablePadding="@dimen/marginplus1"
                android:gravity="start|center_vertical"
                android:drawableLeft="@drawable/ic_share"
                android:layout_height="wrap_content" />

        </LinearLayout>