Text View
に画像スパンの内容があります。 Text View
の場合はellipsize = "end"
とします。 省略記号の後に、画像のスパンが表示されます。Androidでエリプサイズした後でも画像が表示されるtextView
13
A
答えて
-3
はこれを試してみてください:
android:ellipsize="end"
android:singleLine="true"
4
がioschedからEllipsizedTextViewクラスを使用します。 Spannableのビューの使用方法
setText(CharSequence text, BufferType type)
内のテキストを設定するには、あなたがandroid:ellipsize
とandroid:maxLines
を定義する必要がありますまたはあなたにもプログラム的にそれらを設定することができ、あなたのxmlファイルにも
textView.setText(text, TextView.BufferType.SPANNABLE);
を使用することができます。
ここにコードを試してみてください。コードはthis質問
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());
}
}
と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;
}
}
と
<?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
あなたはSpannedEllipsizer、 をオーバーライドする必要がありますactivity_mainから取られ、変更されたあなたはここで完璧な解決を見つけることができます。 https://github.com/lsjwzh/FastTextView/blob/master/widget.FastTextView/src/main/java/com/lsjwzh/widget/text/FastTextView.java
を関連する問題
- 1. Androidの画像が画像表示に表示されない
- 2. Android TextViewのテキスト座標が画面上に表示される
- 3. Androidで画像が表示されず、ScrollViewがスクロールしない
- 4. TextViewエリプサイズ(...)が機能しない
- 5. 画像が表示された後、コレクションビューのセル内でアニメーションを表示
- 6. ページをリフレッシュした後に画像が表示されない
- 7. android textview settextが表示されない
- 8. Androidスタジオ:画像ボタンにフル画像が表示されない
- 9. Androidリストビューで画像がランダムに表示される
- 10. Android:リモートサーバーで画像を更新した後、古い画像を表示している画像ビュー
- 11. setTrackDrawable on android間違った画像が表示される
- 12. 古い画像がiOSアプリケーションで新しい画像に置き換えられた後に表示される
- 13. 画像がwebpack経由で処理された後に表示されない
- 14. JSONのネストされたデータをtextviewで表示するandroid
- 15. 画像をアップロードした後に空白の画面が表示される
- 16. RecyclerViewとTextView自体を更新した後にTextViewが表示されない
- 17. 選択したギャラリー画像が画像表示に表示されません
- 18. javacriptスクリプトを実行した後、ChromeでSVG画像が表示されない
- 19. AFNetworkingでアップロードした後に画像がブラウザに表示されない
- 20. Linkedin:REST経由で共有した後に画像が表示されないApi
- 21. Xcode 8.2.1にアップデートした後で起動画像が表示されない
- 22. Android 4.0では画像が表示されません
- 23. React-Native - Androidエミュレータで画像が表示されない
- 24. 画像上で右クリックしても表示されません
- 25. Androidスタジオ画像を最後にワイルドカードで表示して表示する
- 26. MediaScannerConnection sacnを使用してスキャンした後でも、画像がギャラリーに表示されない
- 27. 画像ソースを変更しても画像が表示されません
- 28. tkinterウィンドウに画像を追加しても画像が表示されない
- 29. Base64エンコードされた画像が表示されました
- 30. フレスコ画を使用してオフラインでAndroidを表示すると画像が表示される
何が問題なのですか? – user3623735
@ user3623735 2012年に尋ねられました –