Put an imageButton in the middle of the text in the textview

时间:2017-05-16 09:32:39

标签: android android-layout

I want to place an image button in the middle of the text in the textview.

for example:

Press the button enter image description here to listen to something blah blah blah blah

I try to use relative layout with textview , imagebutton, textview.

But there is an issue, if the device screen cannot fit in one line, then the last textview will split into 2 lines.

enter image description here

I want the play button to be able to click, so i want the button to be bigger than text. I want the text to switch to next line if cannot fit in one line. Is there any solution to achieve my requirement?

Thanks a lot.

5 个答案:

答案 0 :(得分:1)

您可以使用跨度来实现上述行为:

TextView textView =(TextView) findViewById(R.id.textview);
        SpannableString ss = new SpannableString("abdsadadasdac");
        Drawable d = getResources().getDrawable(android.R.drawable.ic_dialog_email);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
        ss.setSpan(span, 3,6, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        textView.setText(ss);

您需要计算第一部分和第二部分的正确长度,以便在setSpan方法中指定索引。

答案 1 :(得分:0)

try this one

public void addToTxt(Bitmap bitmap){
    SpannableString ss = new SpannableString();
    Drawable d = new BitmapDrawable(getResources(), bitmap);
    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    ss.append("abc"); // Append the text here
    ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
    ss.setSpan(span, 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // start(0) and end (2) will create an image span over abc text
    ss.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    ss.delete(0, 2);
                    tv.setText(ss);
                }
            },0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // this will add a clickable span and on click will delete the span and text
    tv.setText(ss); // this will show your image/clickable span in textview
}


editText.setMovementMethod(LinkMovementMethod.getInstance());

答案 2 :(得分:0)

try this

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:paddingTop="20dp"
        android:text="Press the button " />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:text=" to listen to something blah blah..." />

</LinearLayout>

答案 3 :(得分:0)

使用以下代码,如果您遇到任何问题,请告知我们

<强> XML

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

<强> MainActivity.java

TextView textView = (TextView)findViewById( R.id.TextView );
    SpannableStringBuilder ssb = new SpannableStringBuilder( "Here's an image  , and here's a random text to demonstrate multiple lines sadsadasdasdasdasdsadsadsadasdasdasdasdsadasdasdasdsa m" );
    Bitmap iclauncher = BitmapFactory.decodeResource( getResources(), R.mipmap.ic_launcher );
    ssb.setSpan( new ImageSpan( iclauncher ), 16, 17, Spannable.SPAN_INCLUSIVE_INCLUSIVE );
    textView.setText( ssb, TextView.BufferType.SPANNABLE );

<强>结果

enter image description here

制作textView Clickable的某些部分

您可以使用ClickableSpan执行此操作。以下是示例代码:

TextView myTextView = new TextView(this);
String myString = "Some text [clickable]";
int i1 = myString.indexOf("[");
int i2 = myString.indexOf("]");
myTextView.setMovementMethod(LinkMovementMethod.getInstance());
    myTextView.setText(myString, BufferType.SPANNABLE);
    Spannable mySpannable = (Spannable)myTextView.getText();
    ClickableSpan myClickableSpan = new ClickableSpan()
    {
@Override
public void onClick(View widget) { /* do something */ }
    };
    mySpannable.setSpan(myClickableSpan, i1, i2 + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Reference

答案 4 :(得分:-1)

use android:singleLine="true" or android:maxLines="1"