Mam Text View
, który ma treść obrazu. Podałem ellipsize = "end"
dla Text View
. Po elipsy obraz po nim pojawia się.Zakres obrazu pojawia się nawet po Elipsa w Androidzie textView
13
A
Odpowiedz
-3
Spróbuj tego:
android:ellipsize="end"
android:singleLine="true"
4
Użyj klasę EllipsizedTextView z iosched. Aby ustawić tekst w metodzie
setText(CharSequence text, BufferType type)
widok użyć do Spannable można użyć
textView.setText(text, TextView.BufferType.SPANNABLE);
także w pliku xml trzeba będzie określić android:ellipsize
i android:maxLines
lub można je ustawić programowo jak również.
Umieszczenie kodu, aby go wypróbować. Kod jest modyfikowany, zaczerpnięte z this pytanie
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainActivity extends AppCompatActivity {
private EditText editText;
private Button button;
private EllipsizedTextView textView;
private static final String PATTERN = "\\[\\w+?\\]";
private Pattern pattern;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.edittext);
button = (Button) findViewById(R.id.btn);
textView = (EllipsizedTextView) findViewById(R.id.text);
//programmatic way of setting max lines and ellipsize
textView.setMaxLines(2);
textView.setEllipsize(TextUtils.TruncateAt.END);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
insertImageSpan();
}
});
pattern = Pattern.compile(PATTERN);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
textView.setText(s, TextView.BufferType.SPANNABLE);
}
});
}
private void insertImageSpan(){
insertTextInCurrentPosition(editText,"[face]");
int select = editText.getSelectionStart();
editText.setText(spanText(editText.getText()));
editText.setSelection(select);
}
public CharSequence spanText(CharSequence text) {
SpannableStringBuilder t = null;
if(text instanceof SpannableStringBuilder){
t = (SpannableStringBuilder) text;
}else{
t = new SpannableStringBuilder(text);
}
Matcher m = pattern.matcher(text);
while (m.find()) {
String mResult = m.group();
String key = mResult.substring(1, mResult.length() - 1);
ImageSpan[] spans = t.getSpans(m.start(),m.end(),ImageSpan.class);
if(spans== null || spans.length==0){
try{
ImageSpan span = new ImageSpan(this , R.drawable.ic_launcher);
t.setSpan(span , m.start() , m.end() , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}catch (Exception e){
continue;
}
}
}
return t;
}
public static void insertTextInCurrentPosition(EditText tv, CharSequence str) {
if (tv == null || TextUtils.isEmpty(str)) return;
tv.getText().replace(tv.getSelectionStart() , tv.getSelectionEnd() , str , 0 , str.length());
}
}
i EllipsizedTextView
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* A simple {@link TextView} subclass that uses {@link TextUtils#ellipsize(CharSequence,
* android.text.TextPaint, float, android.text.TextUtils.TruncateAt, boolean,
* android.text.TextUtils.EllipsizeCallback)} to truncate the displayed text. Th
*/
public class EllipsizedTextView extends TextView {
private static final int MAX_ELLIPSIZE_LINES = 100;
private int mMaxLines;
public EllipsizedTextView(Context context) {
this(context, null, 0);
}
public EllipsizedTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public EllipsizedTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// Attribute initialization
final TypedArray a = context.obtainStyledAttributes(attrs, new int[]{
android.R.attr.maxLines
}, defStyle, 0);
mMaxLines = a.getInteger(0, 1);
a.recycle();
}
@Override
public void setText(CharSequence text, BufferType type) {
CharSequence newText = getWidth() == 0 || mMaxLines > MAX_ELLIPSIZE_LINES ? text :
TextUtils.ellipsize(text, getPaint(), getWidth() * mMaxLines,
TextUtils.TruncateAt.END, false, null);
super.setText(newText, type);
}
@Override
protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) {
super.onSizeChanged(width, height, oldWidth, oldHeight);
if (width > 0 && oldWidth != width) {
setText(getText());
}
}
@Override
public void setMaxLines(int maxlines) {
super.setMaxLines(maxlines);
mMaxLines = maxlines;
}
}
i activity_main
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="16dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/edittext" />
<com.anirudha.experiment.EllipsizedTextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:singleLine="false"/>
</RelativeLayout>
</ScrollView>
0
powinny przesłonić SpannedEllipsizer, można znaleźć doskonałe rozwiązanie tutaj: https://github.com/lsjwzh/FastTextView/blob/master/widget.FastTextView/src/main/java/com/lsjwzh/widget/text/FastTextView.java
Powiązane problemy
- 1. Co oznacza elipsa w Androidzie?
- 2. Okno konsoli wciąż pojawia się nawet po ProcessWindowStyle.Hidden;
- 3. Po ponownym utworzeniu aktywności w Androidzie pojawia się okno dialogowe.
- 4. Clickable TextView w Androidzie
- 5. Przewijanie TextView w Androidzie
- 6. Dlaczego klawiatura pojawia się po TabBar?
- 7. Django CKEditor Przesyłanie obrazu nie pojawia się
- 8. dynamicznie aktualizuj TextView w Androidzie
- 9. Autolink wewnątrz TextView w Androidzie
- 10. Symfony2- Logowanie pojawia się ponownie po zalogowaniu
- 11. WPF TextBlock Nakłada Elipsa
- 12. klikalne słowo wewnątrz TextView w Androidzie
- 13. Elipsa nie działa
- 14. pojawia się jako „nie określono” po wykonaniu
- 15. Dlaczego słowo "nie pojawia się" po uruchomieniu?
- 16. Spannable na Androidzie dla textView
- 17. Ponownie wykorzystaj kod TextView w Androidzie XML
- 18. IllegalArgumentException podczas zaznaczania tekstu w Androidzie TextView
- 19. Ustawianie TextView TextAppeareance Programowo w Androidzie
- 20. Obracanie obrazu w Androidzie
- 21. Orientacja aparatu/obrazu w Androidzie
- 22. zmiana rozmiaru czcionki elementu textview w Androidzie
- 23. stwierdzenie EditText jak TextView w Androidzie
- 24. Jak dodać TextView powyżej FloatingActionButton w Androidzie
- 25. Textview Gravity nie działa poprawnie w Androidzie
- 26. elipsy nie pojawia się na text-overflow
- 27. Ograniczająca elipsa
- 28. jak używać kapt w Androidzie Zakres testowy
- 29. Wyświetlacz elipsa przed i po fragmencie w SOLR
- 30. Plik konfiguracyjny pojawia się w folderze bin po opublikowaniu
jakie jest pytanie? – user3623735
@ user3623735 zostało zadane w 2012 roku –