将文本视图中的可绘制图像对齐到中心

时间:2014-05-06 10:15:00

标签: android textview

我有一个文本视图,其中包含图像和一些文本。

以编程方式我将可绘制图像添加到文本视图的左侧

txtIDFav.setCompoundDrawablesWithIntrinsicBounds(R.drawable.fav_enabled, 0, 0, 0);

我已使用

将文本视图对齐到中心
txtIDFav.setGravity(Gravity.CENTER);

现在textview中的文本与中心对齐。但图像在左侧。

例如:[image] _____________________ mytext

使用_来提供空间,因为它是不允许的。

我想将图像与文本对齐。

例如: _____________________ [image] mytext

请建议。

谢谢并感谢。

6 个答案:

答案 0 :(得分:3)

试试这个,它应该有效:

    Spannable span = new SpannableString("  " + txtIDFav.getText());  // or set your text manually

    Drawable drawable;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    {
        drawable = getResources().getDrawable(R.drawable.fav_enabled, getContext().getTheme());
    }
    else
    {
        drawable = getResources().getDrawable(R.drawable.fav_enabled);
    }

    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    ImageSpan imageSpan = new ImageSpan(drawable);
    span.setSpan(imageSpan, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

    txtIDFav.setText(span);

答案 1 :(得分:1)

我也面临着相同的问题,但是我不想分别使用Button和TextView,也不想使用Java代码来实现它。所以我找到了。我知道我要晚解决。但这对您有帮助

<FrameLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_margin="20dp"
            android:background="@drawable/next_btn_bg_rounded_corners">


            <TextView
                android:id="@+id/btn_restore_purchases"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@null"
                android:drawableLeft="@mipmap/restore"
                android:drawablePadding="10dp"
                android:drawableStart="@mipmap/restore"
                android:text="@string/restore_purchases"
                android:textAllCaps="false"
                android:textColor="@color/white" />
        </FrameLayout>

icon with text in center

答案 2 :(得分:0)

使用SpannableString将drawable嵌入文本中。

        SpannableStringBuilder textspan = new SpannableStringBuilder("  " + yourText);
        Drawable icon = getResources().getDrawable(R.drawable.fav_enabled);
        // jump drawable to the text view's state (if it is a state drawable)
        icon.setState(textView.getBackground().getState());  // match the background state
        textspan.setSpan(new ImageSpan(icon, ImageSpan.ALIGN_BOTTOM), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(textspan);

答案 3 :(得分:0)

我认为是因为textView的宽度设置为match_parent或fill_parent。尝试将textView width设置为wrap_content,并将整个textView不仅放在文本中心。

布局:

sudo chown -R $USER /usr/local/share/systemtap

的活动:

sudo chgrp -R staff /usr/local/share/systemtap
sudo chmod -R g+w /usr/local/share/systemtap

Result

答案 4 :(得分:-1)

无法在文本视图的背景中设置图像,而不是这样你可以在textview上设置你的drawable像这样

<强>机器人:drawableRight =&#34; @可绘制/ ic_launcher&#34;

&#34;或&#34;

<强>机器人:drawableleft =&#34; @可绘制/ ic_launcher&#34;

&#34;或&#34;

<强>机器人:drawableBottom =&#34; @可绘制/ ic_launcher&#34;

仅针对您的问题解决方案,您可以通过这种方式在framelayout中使用texview

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<FrameLayout
    android:layout_width="match_parent"
    android:background="@drawable/ic_launcher"
    android:layout_gravity="center"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView1"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</FrameLayout>

答案 5 :(得分:-1)

您必须编写如下代码: -

$BASH
相关问题