Align drawable image inside a text view to center

2019-02-12 05:07发布

I have a textview which holds an image and some text.

programmatically i have added the drawable image to the left side of text view

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

i have aligned the text view to center using

txtIDFav.setGravity(Gravity.CENTER);

Now the text in the textview is aligned to center. but the image is on the left side.

eg: [image]_____________________ mytext

used the _ to give space as its not allowed.

I want to align the image to center along with the text.

eg: _____________________[image]mytext

Please advice.

Thanks and appreciate.

6条回答
神经病院院长
2楼-- · 2019-02-12 05:16

it is not possible to set image in background of text view instead of this you can set your drawable on textview like this

android:drawableRight="@drawable/ic_launcher"

"or"

android:drawableleft="@drawable/ic_launcher"

"or"

android:drawableBottom="@drawable/ic_launcher"

only for your problem's solution you can use texview inside the framelayout in this way

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

查看更多
爷、活的狠高调
3楼-- · 2019-02-12 05:17

You have to write code like this:-

txtIDFav.setGravity(Gravity.CENTER | Gravity.RIGHT);
查看更多
啃猪蹄的小仙女
4楼-- · 2019-02-12 05:19

Embed the drawable in your text by using a SpannableString.

        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);
查看更多
Anthone
5楼-- · 2019-02-12 05:23

I assume it is happened because the width of textView is set to match_parent or fill_parent. Try setting textView width to wrap_content and center the whole textView not only text.

Layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textViewTest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Test"
        android:textSize="28sp" />
</FrameLayout>

Activity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textViewTest=(TextView)findViewById(R.id.textViewTest);
        textViewTest.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.ic_launcher, 0, 0, 0);
    }
}

Result

查看更多
爷的心禁止访问
6楼-- · 2019-02-12 05:27

I was also facing the same issue, But I don't want to use Button and TextView separately and also I don't want to make it using java code. So I found this. I know I am giving solution late. But it helps you

<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

查看更多
看我几分像从前
7楼-- · 2019-02-12 05:37

Try this, it should work:

    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);
查看更多
登录 后发表回答