簡體   English   中英

將imageButton放在textview中的文本中間

[英]Put an imageButton in the middle of the text in the textview

我想在textview的文本中間放置一個圖像按鈕。

例如:

按下按鈕 在此輸入圖像描述 聽一些等等等等等等

我嘗試使用textview,imagebutton,textview的相對布局。

但是有一個問題,如果設備屏幕不能放在一行,那么最后一個textview將分成2行。

在此輸入圖像描述

我希望播放按鈕能夠單擊,所以我希望按鈕比文本大。 如果不能放在一行,我希望文本切換到下一行。 有沒有解決方案來實現我的要求?

非常感謝。

您可以使用跨度來實現上述行為:

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方法中指定索引。

試試這個

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());

嘗試這個

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

如果您遇到任何問題,請使用以下代碼告訴我

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

結果

在此輸入圖像描述

制作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);

參考

使用android:singleLine="true"android:maxLines="1"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM