[英]Put an imageButton in the middle of the text in the textview
您可以使用跨度來實現上述行為:
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.