TextView MaxLines、Movement Method、およびEllipize -- android フィールド と textview フィールド 関連 問題

TextView maxLines, movement method and ellipsize












11
vote

問題

日本語

maxLines:5 および ellipsize:end が適用されている TextView があります。<コード> setMovementMethod(LinkMovementMethod.getInstance()) を使用しています。 CODE> TextViewリンクをクリック可能(HTMLコンテンツ)。

上記のすべての組み合わせは、切り捨てられているテキストを無効にし、添付される '...'サフィックスを追加します。

何が悪いのか、そしてそれを回避する方法?

移動方法を設定せずに、すべてが期待どおりに機能します。

賞金に関する更新: 手動でellipsesを設定する以外の解を探す

英語

I have a TextView with maxLines:5 and ellipsize:end applied, I'm also using setMovementMethod(LinkMovementMethod.getInstance()) on the TextView to make links clickable (HTML content).

The combination of all of the above disables the text being truncated and the '...' suffix to be appended.

Any idea what goes wrong and how to work around it?

Without setting the movement method, everything works as expected.

Update regarding bounty: looking for solution other than manually setting ellipses

</div
     

回答リスト

5
 
vote

これはこれに遅れています。

これはこの

のためにほとんど働きかけません

肉体

<事前> <コード> public class MainActivity extends AppCompatActivity { TextView htmlTextView; CustomEllipsizeTextView customEllipsizeTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); htmlTextView = findViewById(R.id.htmlTextView1); customEllipsizeTextView = findViewById(R.id.customEllipsizeTextView); String value = "Hello this is a dummy textview"; String myText = "You can visit my Profile in <a href="https://stackoverflow.com/users/7666442/nilesh-rathod?tab=profile">stackoverflow</a> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis bibendum mattis risus eget pulvinar. Praesenttingd" + " commodo erat enim, id564654 congue sem tristique vitae. Proin vitae accumsan justo, ut imperdiet Mauris neque nibh, hendrerit id tortor vel, congue sagittis odio. Morbi elementum lobortis maximus. Etiam sit amet porttitor massa. Fusce sed magna quis arcu tincidunt finibus vitae id erat. " + "commodo erat enim, id54654 congue sem tristique vitae. Proin vitae accumsan commodo erat enim, id congue sem tristique vitae. Proin vitae accumsan Pellentesque massa mi, imperdiet eget accums "; SpannableString spanText2 = new SpannableString(myText); htmlTextView.setText(value); if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { customEllipsizeTextView.setText(Html.fromHtml(spanText2.toString(), Html.FROM_HTML_MODE_LEGACY)); } else { customEllipsizeTextView.setText(Html.fromHtml(spanText2.toString())); } htmlTextView.setMovementMethod(LinkMovementMethod.getInstance()); customEllipsizeTextView.setMovementMethod(LinkMovementMethod.getInstance()); customEllipsizeTextView.setOnTouchListener(new TouchTextView(spanText2)); } }

Layout.activity_main

<事前> <コード> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".FirstFragment"> <TextView android:id="@+id/htmlTextView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLines="1" android:text="@string/link_text" /> <neel.com.demo.CustomEllipsizeTextView android:id="@+id/customEllipsizeTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="end" android:linksClickable="true" android:maxLines="5" android:padding="5dp" android:visibility="visible" /> </LinearLayout>

CustomELLIPIZETEXTVIEW

<事前> <コード> public class CustomEllipsizeTextView extends android.support.v7.widget.AppCompatTextView { public CustomEllipsizeTextView(Context context) { super(context); } public CustomEllipsizeTextView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomEllipsizeTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { StaticLayout layout = null; Field field = null; try { Field staticField = DynamicLayout.class.getDeclaredField("sStaticLayout"); staticField.setAccessible(true); layout = (StaticLayout) staticField.get(DynamicLayout.class); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } if (layout != null) { try { field = StaticLayout.class.getDeclaredField("mMaximumVisibleLineCount"); field.setAccessible(true); field.setInt(layout, getMaxLines()); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (layout != null && field != null) { try { field.setInt(layout, Integer.MAX_VALUE); } catch (IllegalAccessException e) { e.printStackTrace(); } } } }

TouchTextView

<事前> <コード> public class TouchTextView implements View.OnTouchListener { Spannable spannable; public TouchTextView (Spannable spannable){ this.spannable = spannable; } @Override public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); if(!(v instanceof TextView)){ return false; } TextView textView = (TextView) v; if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) { int x = (int) event.getX(); int y = (int) event.getY(); x -= textView.getTotalPaddingLeft(); y -= textView.getTotalPaddingTop(); x += textView.getScrollX(); y += textView.getScrollY(); Layout layout = textView.getLayout(); int line = layout.getLineForVertical(y); int off = layout.getOffsetForHorizontal(line, x); ClickableSpan[] link = spannable.getSpans(off, off, ClickableSpan.class); if (link.length != 0) { if (action == MotionEvent.ACTION_UP) { link[0].onClick(textView); } else if (action == MotionEvent.ACTION_DOWN) { Selection.setSelection(spannable, spannable.getSpanStart(link[0]), spannable.getSpanEnd(link[0])); } return true; } else { Selection.removeSelection(spannable); } } return false; } }

出力

画像の説明を入力します。ここで

これは説明です:

デバッグされたTextViewがあり、次のものが見つかりました。

SO LINKMOVEMENTMETHOD()を使用すると、テキストはスパンタブルとして機能しています。他の場合それは文字列です。

TextView

内部の次の条件があります。 <事前> <コード> if (mText instanceof Spannable) { //executes incase of LinkMovementMethod result = new DynamicLayout(mText, mTransformed, mTextPaint, wantWidth, alignment, mTextDir, mSpacingMult, mSpacingAdd, mIncludePad, mBreakStrategy, mHyphenationFrequency, mJustificationMode, getKeyListener() == null ? effectiveEllipsize : null, ellipsisWidth); } else { //executes without any movementmethod ..create StaticLayout }

SO DynamicLayoutは、TextをレンダリングするためにStaticLayoutを内部的に呼び出しますが、DynamicLayoutからの mMaximumVisibleLineCount はデフォルトの Integer.MAX_VALUE です。しかし、StaticLayoutを文字列から作成するときは、実際に mMaximumVisibleLineCount maxLines として設定しています。この mMaximumVisibleLineCount は、ELLipSizeを表示するために使用されます。だからこそ「...」が表示されていません。

行数を表示するには、次のコードが機能しています

<事前> <コード> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".FirstFragment"> <TextView android:id="@+id/htmlTextView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLines="1" android:text="@string/link_text" /> <neel.com.demo.CustomEllipsizeTextView android:id="@+id/customEllipsizeTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="end" android:linksClickable="true" android:maxLines="5" android:padding="5dp" android:visibility="visible" /> </LinearLayout> 0

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".FirstFragment"> <TextView android:id="@+id/htmlTextView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLines="1" android:text="@string/link_text" /> <neel.com.demo.CustomEllipsizeTextView android:id="@+id/customEllipsizeTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="end" android:linksClickable="true" android:maxLines="5" android:padding="5dp" android:visibility="visible" /> </LinearLayout> 1 はどちらもmaxlinesに設定されますが、 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".FirstFragment"> <TextView android:id="@+id/htmlTextView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLines="1" android:text="@string/link_text" /> <neel.com.demo.CustomEllipsizeTextView android:id="@+id/customEllipsizeTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="end" android:linksClickable="true" android:maxLines="5" android:padding="5dp" android:visibility="visible" /> </LinearLayout> 2 は、MovhodMethodなしで1つのMaxlinesになり、移動Methodでは元の文字列の行数になります。 / P>

 

Sorry I'm late on this one.

Here is little work around for this

MainActivity

public class MainActivity extends AppCompatActivity {       TextView htmlTextView;      CustomEllipsizeTextView  customEllipsizeTextView;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);           htmlTextView = findViewById(R.id.htmlTextView1);          customEllipsizeTextView = findViewById(R.id.customEllipsizeTextView);          String value = "Hello this is a dummy textview";          String myText = "You can visit my  Profile in <a href="https://stackoverflow.com/users/7666442/nilesh-rathod?tab=profile">stackoverflow</a> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis bibendum mattis risus eget pulvinar. Praesenttingd" +                 " commodo erat enim, id564654 congue sem tristique vitae. Proin vitae accumsan justo, ut imperdiet Mauris neque nibh, hendrerit id tortor vel, congue sagittis odio. Morbi elementum lobortis maximus. Etiam sit amet porttitor massa. Fusce sed magna quis arcu tincidunt finibus vitae id erat. " +                 "commodo erat enim, id54654 congue sem tristique vitae. Proin vitae accumsan commodo erat enim, id congue sem tristique vitae. Proin vitae accumsan Pellentesque massa mi, imperdiet eget accums ";           SpannableString spanText2 = new SpannableString(myText);         htmlTextView.setText(value);          if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {             customEllipsizeTextView.setText(Html.fromHtml(spanText2.toString(), Html.FROM_HTML_MODE_LEGACY));         } else {             customEllipsizeTextView.setText(Html.fromHtml(spanText2.toString()));         }          htmlTextView.setMovementMethod(LinkMovementMethod.getInstance());         customEllipsizeTextView.setMovementMethod(LinkMovementMethod.getInstance());         customEllipsizeTextView.setOnTouchListener(new TouchTextView(spanText2));      }  } 

layout.activity_main

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     tools:context=".FirstFragment">        <TextView         android:id="@+id/htmlTextView1"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:maxLines="1"         android:text="@string/link_text" />      <neel.com.demo.CustomEllipsizeTextView         android:id="@+id/customEllipsizeTextView"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:ellipsize="end"         android:linksClickable="true"         android:maxLines="5"         android:padding="5dp"         android:visibility="visible" />   </LinearLayout> 

CustomEllipsizeTextView

public class CustomEllipsizeTextView extends android.support.v7.widget.AppCompatTextView {     public CustomEllipsizeTextView(Context context) {         super(context);     }      public CustomEllipsizeTextView(Context context, AttributeSet attrs) {         super(context, attrs);     }      public CustomEllipsizeTextView(Context context, AttributeSet attrs, int defStyleAttr) {         super(context, attrs, defStyleAttr);     }       @Override     protected void onDetachedFromWindow() {         super.onDetachedFromWindow();     }       @Override     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {         StaticLayout layout = null;         Field field = null;         try {             Field staticField = DynamicLayout.class.getDeclaredField("sStaticLayout");             staticField.setAccessible(true);             layout = (StaticLayout) staticField.get(DynamicLayout.class);         } catch (NoSuchFieldException e) {             e.printStackTrace();         } catch (IllegalAccessException e) {             e.printStackTrace();         }          if (layout != null) {             try {                 field = StaticLayout.class.getDeclaredField("mMaximumVisibleLineCount");                 field.setAccessible(true);                 field.setInt(layout, getMaxLines());             } catch (NoSuchFieldException e) {                 e.printStackTrace();             } catch (IllegalAccessException e) {                 e.printStackTrace();             }         }         super.onMeasure(widthMeasureSpec, heightMeasureSpec);         if (layout != null && field != null) {             try {                 field.setInt(layout, Integer.MAX_VALUE);             } catch (IllegalAccessException e) {                 e.printStackTrace();             }         }     } } 

TouchTextView

public class TouchTextView implements View.OnTouchListener {     Spannable spannable;      public TouchTextView (Spannable spannable){         this.spannable = spannable;     }     @Override     public boolean onTouch(View v, MotionEvent event) {         int action = event.getAction();         if(!(v instanceof TextView)){             return false;         }         TextView textView  = (TextView) v;         if (action == MotionEvent.ACTION_UP ||                 action == MotionEvent.ACTION_DOWN) {             int x = (int) event.getX();             int y = (int) event.getY();              x -= textView.getTotalPaddingLeft();             y -= textView.getTotalPaddingTop();              x += textView.getScrollX();             y += textView.getScrollY();              Layout layout = textView.getLayout();             int line = layout.getLineForVertical(y);             int off = layout.getOffsetForHorizontal(line, x);              ClickableSpan[] link = spannable.getSpans(off, off, ClickableSpan.class);              if (link.length != 0) {                 if (action == MotionEvent.ACTION_UP) {                     link[0].onClick(textView);                 } else if (action == MotionEvent.ACTION_DOWN) {                     Selection.setSelection(spannable,                             spannable.getSpanStart(link[0]),                             spannable.getSpanEnd(link[0]));                 }                  return true;             } else {                 Selection.removeSelection(spannable);             }         }          return false;     } } 

OUTPUT

enter image description here

Here is the explanation :

I have debugged TextView and found out the following :

So when you use LinkMovementMethod() actually text is acting as Spannable. In other case it is String.

There is one following condition inside TextView

if (mText instanceof Spannable) { //executes incase of LinkMovementMethod             result = new DynamicLayout(mText, mTransformed, mTextPaint, wantWidth,                     alignment, mTextDir, mSpacingMult, mSpacingAdd, mIncludePad,                     mBreakStrategy, mHyphenationFrequency, mJustificationMode,                     getKeyListener() == null ? effectiveEllipsize : null, ellipsisWidth);         } else {     //executes without any movementmethod ..create StaticLayout } 

So DynamicLayout internally calls StaticLayout to render text, but it is not setting mMaximumVisibleLineCountinside StaticLayout when coming from DynamicLayout so it is default Integer.MAX_VALUE. But when creating StaticLayout from String, it is actually setting mMaximumVisibleLineCount as maxLines. This mMaximumVisibleLineCount is used for displaying ellipsize. That's why "..." is not displaying.

For displaying the number of lines, the following code works

if (mMaxMode == LINES && mLayout.getLineCount() > mMaximum) {             unpaddedHeight = Math.min(unpaddedHeight, mLayout.getLineTop(mMaximum));         } 

mMaximum will be set to maxLines in both case, but mLayout.getLineCount() will be maxLines for one without MovementMethod and for with MovementMethod it will be number of lines of original string

</div
 
 
1
 
vote

このコードを試してください。

<事前> <コード> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".FirstFragment"> <TextView android:id="@+id/htmlTextView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLines="1" android:text="@string/link_text" /> <neel.com.demo.CustomEllipsizeTextView android:id="@+id/customEllipsizeTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="end" android:linksClickable="true" android:maxLines="5" android:padding="5dp" android:visibility="visible" /> </LinearLayout> 3
 

Try this code.

           <TextView             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:textColor="@color/white"             android:textStyle="bold"             android:textSize="20dp"             android:id="@+id/txtTitle"             android:text="" />             String message="<font color='gray'>"+"YOUR CONTENT"+ "<br>" +"<font color='cyan'>"+"<font size='5'>"+" "+"</font>";            txtTitle.setBackgroundColor(Color.TRANSPARENT);            txtTitle.setText(message); 
</div
 
 
1
 
vote

このコードを試してください。

XML

<事前> <コード> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".FirstFragment"> <TextView android:id="@+id/htmlTextView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLines="1" android:text="@string/link_text" /> <neel.com.demo.CustomEllipsizeTextView android:id="@+id/customEllipsizeTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="end" android:linksClickable="true" android:maxLines="5" android:padding="5dp" android:visibility="visible" /> </LinearLayout> 4

Javaファイルコード

<事前> <コード> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".FirstFragment"> <TextView android:id="@+id/htmlTextView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLines="1" android:text="@string/link_text" /> <neel.com.demo.CustomEllipsizeTextView android:id="@+id/customEllipsizeTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="end" android:linksClickable="true" android:maxLines="5" android:padding="5dp" android:visibility="visible" /> </LinearLayout> 5

選択したテキストをタッチします

<事前> <コード> Integer.MAX_VALUE16
 

Try this code.

XML

<TextView  android:id="@+id/tvcondition1"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:textColor="@color/white"  android:textStyle="bold"  android:ellipsize="end"  android:maxLines="5"  android:textSize="15sp" /> 

Java file Code

    String strTeamsCondition = "<b><u> Terms &amp; Condition </u></b>";     String conditionlimit = "Terms & Condition";     String strConditionFirstLine = getString(R.string.condition_1);     String condition_1_1 = getString(R.string.condition_1_1);     String htmlAsString2 = strConditionFirstLine + strTeamsCondition + condition_1_1;     Spanned htmlAsSpannedCondition = Html.fromHtml(htmlAsString2);       tvcondition1.setText(htmlAsSpannedCondition);     Spannable spanText = new SpannableString(htmlAsSpannedCondition);     spanText.setSpan(new MyClickableSpan(htmlAsSpannedCondition), strConditionFirstLine.length(), strConditionFirstLine.length() + conditionlimit.length() + 1, 0);     tvcondition1.setText(spanText);     tvcondition1.setMovementMethod(LinkMovementMethod.getInstance()); 

Touch Selected Text

 class MyClickableSpan extends ClickableSpan {         public MyClickableSpan(Spanned string) {             super();         }          public void onClick(View tv) {             Toast.makeText(getApplicationContext(), "Thanks for the click!",                 Toast.LENGTH_SHORT).show();          }          public void updateDrawState(TextPaint ds) {              ds.setColor(getResources().getColor(R.color.black));             ds.setUnderlineText(true); // set to false to remove underline         }      } 
</div
 
 
0
 
vote
<事前> <コード> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".FirstFragment"> <TextView android:id="@+id/htmlTextView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLines="1" android:text="@string/link_text" /> <neel.com.demo.CustomEllipsizeTextView android:id="@+id/customEllipsizeTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="end" android:linksClickable="true" android:maxLines="5" android:padding="5dp" android:visibility="visible" /> </LinearLayout> 7
 
  <TextView         android:id="@+id/html"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:maxLines="5"         android:ellipsize="end"/>          //inside your activity         public class A extends AppCompatActivity{                 TextView html = (TextView) view.findViewById(R.id.html);                html.setText(Html.fromHtml("this is an example <a href="www.google.com">google</a> link to google"));                html.setMovementMethod(LinkMovementMethod.getInstance());                .                .                .          } 
</div
 
 
 
 
0
 
vote

これを試してください。

ウェイ1 。 <コード> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".FirstFragment"> <TextView android:id="@+id/htmlTextView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLines="1" android:text="@string/link_text" /> <neel.com.demo.CustomEllipsizeTextView android:id="@+id/customEllipsizeTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="end" android:linksClickable="true" android:maxLines="5" android:padding="5dp" android:visibility="visible" /> </LinearLayout> 8 および<コード> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".FirstFragment"> <TextView android:id="@+id/htmlTextView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLines="1" android:text="@string/link_text" /> <neel.com.demo.CustomEllipsizeTextView android:id="@+id/customEllipsizeTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="end" android:linksClickable="true" android:maxLines="5" android:padding="5dp" android:visibility="visible" /> </LinearLayout> 9

を使用

public class CustomEllipsizeTextView extends android.support.v7.widget.AppCompatTextView { public CustomEllipsizeTextView(Context context) { super(context); } public CustomEllipsizeTextView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomEllipsizeTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { StaticLayout layout = null; Field field = null; try { Field staticField = DynamicLayout.class.getDeclaredField("sStaticLayout"); staticField.setAccessible(true); layout = (StaticLayout) staticField.get(DynamicLayout.class); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } if (layout != null) { try { field = StaticLayout.class.getDeclaredField("mMaximumVisibleLineCount"); field.setAccessible(true); field.setInt(layout, getMaxLines()); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (layout != null && field != null) { try { field.setInt(layout, Integer.MAX_VALUE); } catch (IllegalAccessException e) { e.printStackTrace(); } } } } 0 public class CustomEllipsizeTextView extends android.support.v7.widget.AppCompatTextView { public CustomEllipsizeTextView(Context context) { super(context); } public CustomEllipsizeTextView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomEllipsizeTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { StaticLayout layout = null; Field field = null; try { Field staticField = DynamicLayout.class.getDeclaredField("sStaticLayout"); staticField.setAccessible(true); layout = (StaticLayout) staticField.get(DynamicLayout.class); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } if (layout != null) { try { field = StaticLayout.class.getDeclaredField("mMaximumVisibleLineCount"); field.setAccessible(true); field.setInt(layout, getMaxLines()); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (layout != null && field != null) { try { field.setInt(layout, Integer.MAX_VALUE); } catch (IllegalAccessException e) { e.printStackTrace(); } } } } 1 を設定しませんでした。それは仕事です。

あなたのXMLコードに

<事前> <コード> public class CustomEllipsizeTextView extends android.support.v7.widget.AppCompatTextView { public CustomEllipsizeTextView(Context context) { super(context); } public CustomEllipsizeTextView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomEllipsizeTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { StaticLayout layout = null; Field field = null; try { Field staticField = DynamicLayout.class.getDeclaredField("sStaticLayout"); staticField.setAccessible(true); layout = (StaticLayout) staticField.get(DynamicLayout.class); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } if (layout != null) { try { field = StaticLayout.class.getDeclaredField("mMaximumVisibleLineCount"); field.setAccessible(true); field.setInt(layout, getMaxLines()); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (layout != null && field != null) { try { field.setInt(layout, Integer.MAX_VALUE); } catch (IllegalAccessException e) { e.printStackTrace(); } } } } 2

それからあなたのJavaコードの

<事前> <コード> public class CustomEllipsizeTextView extends android.support.v7.widget.AppCompatTextView { public CustomEllipsizeTextView(Context context) { super(context); } public CustomEllipsizeTextView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomEllipsizeTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { StaticLayout layout = null; Field field = null; try { Field staticField = DynamicLayout.class.getDeclaredField("sStaticLayout"); staticField.setAccessible(true); layout = (StaticLayout) staticField.get(DynamicLayout.class); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } if (layout != null) { try { field = StaticLayout.class.getDeclaredField("mMaximumVisibleLineCount"); field.setAccessible(true); field.setInt(layout, getMaxLines()); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (layout != null && field != null) { try { field.setInt(layout, Integer.MAX_VALUE); } catch (IllegalAccessException e) { e.printStackTrace(); } } } } 3

ウェイ2 。 XMLコード

で<コード> public class CustomEllipsizeTextView extends android.support.v7.widget.AppCompatTextView { public CustomEllipsizeTextView(Context context) { super(context); } public CustomEllipsizeTextView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomEllipsizeTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { StaticLayout layout = null; Field field = null; try { Field staticField = DynamicLayout.class.getDeclaredField("sStaticLayout"); staticField.setAccessible(true); layout = (StaticLayout) staticField.get(DynamicLayout.class); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } if (layout != null) { try { field = StaticLayout.class.getDeclaredField("mMaximumVisibleLineCount"); field.setAccessible(true); field.setInt(layout, getMaxLines()); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (layout != null && field != null) { try { field.setInt(layout, Integer.MAX_VALUE); } catch (IllegalAccessException e) { e.printStackTrace(); } } } } 4 を使用してください。 <事前> <コード> public class CustomEllipsizeTextView extends android.support.v7.widget.AppCompatTextView { public CustomEllipsizeTextView(Context context) { super(context); } public CustomEllipsizeTextView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomEllipsizeTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { StaticLayout layout = null; Field field = null; try { Field staticField = DynamicLayout.class.getDeclaredField("sStaticLayout"); staticField.setAccessible(true); layout = (StaticLayout) staticField.get(DynamicLayout.class); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } if (layout != null) { try { field = StaticLayout.class.getDeclaredField("mMaximumVisibleLineCount"); field.setAccessible(true); field.setInt(layout, getMaxLines()); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (layout != null && field != null) { try { field.setInt(layout, Integer.MAX_VALUE); } catch (IllegalAccessException e) { e.printStackTrace(); } } } } 5

あなたのJavaコードで。

<事前> <コード> public class CustomEllipsizeTextView extends android.support.v7.widget.AppCompatTextView { public CustomEllipsizeTextView(Context context) { super(context); } public CustomEllipsizeTextView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomEllipsizeTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { StaticLayout layout = null; Field field = null; try { Field staticField = DynamicLayout.class.getDeclaredField("sStaticLayout"); staticField.setAccessible(true); layout = (StaticLayout) staticField.get(DynamicLayout.class); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } if (layout != null) { try { field = StaticLayout.class.getDeclaredField("mMaximumVisibleLineCount"); field.setAccessible(true); field.setInt(layout, getMaxLines()); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (layout != null && field != null) { try { field.setInt(layout, Integer.MAX_VALUE); } catch (IllegalAccessException e) { e.printStackTrace(); } } } } 6

ウェイ3 。コード内の<コード> public class CustomEllipsizeTextView extends android.support.v7.widget.AppCompatTextView { public CustomEllipsizeTextView(Context context) { super(context); } public CustomEllipsizeTextView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomEllipsizeTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { StaticLayout layout = null; Field field = null; try { Field staticField = DynamicLayout.class.getDeclaredField("sStaticLayout"); staticField.setAccessible(true); layout = (StaticLayout) staticField.get(DynamicLayout.class); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } if (layout != null) { try { field = StaticLayout.class.getDeclaredField("mMaximumVisibleLineCount"); field.setAccessible(true); field.setInt(layout, getMaxLines()); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (layout != null && field != null) { try { field.setInt(layout, Integer.MAX_VALUE); } catch (IllegalAccessException e) { e.printStackTrace(); } } } } 7 を使用してください。

<事前> <コード> public class CustomEllipsizeTextView extends android.support.v7.widget.AppCompatTextView { public CustomEllipsizeTextView(Context context) { super(context); } public CustomEllipsizeTextView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomEllipsizeTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { StaticLayout layout = null; Field field = null; try { Field staticField = DynamicLayout.class.getDeclaredField("sStaticLayout"); staticField.setAccessible(true); layout = (StaticLayout) staticField.get(DynamicLayout.class); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } if (layout != null) { try { field = StaticLayout.class.getDeclaredField("mMaximumVisibleLineCount"); field.setAccessible(true); field.setInt(layout, getMaxLines()); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (layout != null && field != null) { try { field.setInt(layout, Integer.MAX_VALUE); } catch (IllegalAccessException e) { e.printStackTrace(); } } } } 8

編集

スクロールすることはできますが、 public class CustomEllipsizeTextView extends android.support.v7.widget.AppCompatTextView { public CustomEllipsizeTextView(Context context) { super(context); } public CustomEllipsizeTextView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomEllipsizeTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { StaticLayout layout = null; Field field = null; try { Field staticField = DynamicLayout.class.getDeclaredField("sStaticLayout"); staticField.setAccessible(true); layout = (StaticLayout) staticField.get(DynamicLayout.class); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } if (layout != null) { try { field = StaticLayout.class.getDeclaredField("mMaximumVisibleLineCount"); field.setAccessible(true); field.setInt(layout, getMaxLines()); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (layout != null && field != null) { try { field.setInt(layout, Integer.MAX_VALUE); } catch (IllegalAccessException e) { e.printStackTrace(); } } } } 9 を表示しません。

を追加できます <事前> <コード> public class TouchTextView implements View.OnTouchListener { Spannable spannable; public TouchTextView (Spannable spannable){ this.spannable = spannable; } @Override public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); if(!(v instanceof TextView)){ return false; } TextView textView = (TextView) v; if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) { int x = (int) event.getX(); int y = (int) event.getY(); x -= textView.getTotalPaddingLeft(); y -= textView.getTotalPaddingTop(); x += textView.getScrollX(); y += textView.getScrollY(); Layout layout = textView.getLayout(); int line = layout.getLineForVertical(y); int off = layout.getOffsetForHorizontal(line, x); ClickableSpan[] link = spannable.getSpans(off, off, ClickableSpan.class); if (link.length != 0) { if (action == MotionEvent.ACTION_UP) { link[0].onClick(textView); } else if (action == MotionEvent.ACTION_DOWN) { Selection.setSelection(spannable, spannable.getSpanStart(link[0]), spannable.getSpanEnd(link[0])); } return true; } else { Selection.removeSelection(spannable); } } return false; } } 0
 

Try this.

Way 1. use setMovementMethod and Html.fromHtml

I didn't set maxLines and ellipsize. It 's work ok .

In your XML code

<TextView       android:id="@+id/tv_html"       android:ellipsize="end"     android:maxLines="5"     android:layout_width="wrap_content"       android:layout_height="wrap_content" />  

Then In your java code

 TextView tv_html = (TextView) findViewById(R.id.tv_html);  tv_html.setText(Html.fromHtml("google:" + "<a href='https://www.google.com.hk'>link to it</a> "));  tv_html.setMovementMethod(LinkMovementMethod.getInstance());// make it active 

Way 2. use android:autoLink="all" in the XML code

<TextView       android:id="@+id/tv_html"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:autoLink="all"/>   

Then in your java code.

TextView tv_html = (TextView) findViewById(R.id.tv_html); tv_html.setText("google: https://www.google.com.hk")); 

Way 3. use SpannableString in the code.

TextView tv_html = (TextView) findViewById(R.id.tv_html);  SpannableString ss = new SpannableString("google: link to google");   ss.setSpan(new URLSpan("https://www.google.com.hk"), 8, 22, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);   tv_html.setText(ss);   tv_html .setMovementMethod(LinkMovementMethod.getInstance());   

Edit

It can scroll but not show the ....

You can add

 android:scrollbars="vertical" 
</div
 
 
   
   
0
 
vote

これに対する起こり得る回避策は

<事前> <コード> public class TouchTextView implements View.OnTouchListener { Spannable spannable; public TouchTextView (Spannable spannable){ this.spannable = spannable; } @Override public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); if(!(v instanceof TextView)){ return false; } TextView textView = (TextView) v; if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) { int x = (int) event.getX(); int y = (int) event.getY(); x -= textView.getTotalPaddingLeft(); y -= textView.getTotalPaddingTop(); x += textView.getScrollX(); y += textView.getScrollY(); Layout layout = textView.getLayout(); int line = layout.getLineForVertical(y); int off = layout.getOffsetForHorizontal(line, x); ClickableSpan[] link = spannable.getSpans(off, off, ClickableSpan.class); if (link.length != 0) { if (action == MotionEvent.ACTION_UP) { link[0].onClick(textView); } else if (action == MotionEvent.ACTION_DOWN) { Selection.setSelection(spannable, spannable.getSpanStart(link[0]), spannable.getSpanEnd(link[0])); } return true; } else { Selection.removeSelection(spannable); } } return false; } } 1

Spannableクラス:

<事前> <コード> public class TouchTextView implements View.OnTouchListener { Spannable spannable; public TouchTextView (Spannable spannable){ this.spannable = spannable; } @Override public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); if(!(v instanceof TextView)){ return false; } TextView textView = (TextView) v; if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) { int x = (int) event.getX(); int y = (int) event.getY(); x -= textView.getTotalPaddingLeft(); y -= textView.getTotalPaddingTop(); x += textView.getScrollX(); y += textView.getScrollY(); Layout layout = textView.getLayout(); int line = layout.getLineForVertical(y); int off = layout.getOffsetForHorizontal(line, x); ClickableSpan[] link = spannable.getSpans(off, off, ClickableSpan.class); if (link.length != 0) { if (action == MotionEvent.ACTION_UP) { link[0].onClick(textView); } else if (action == MotionEvent.ACTION_DOWN) { Selection.setSelection(spannable, spannable.getSpanStart(link[0]), spannable.getSpanEnd(link[0])); } return true; } else { Selection.removeSelection(spannable); } } return false; } } 2

それを呼び出す:

<事前> <コード> public class TouchTextView implements View.OnTouchListener { Spannable spannable; public TouchTextView (Spannable spannable){ this.spannable = spannable; } @Override public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); if(!(v instanceof TextView)){ return false; } TextView textView = (TextView) v; if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) { int x = (int) event.getX(); int y = (int) event.getY(); x -= textView.getTotalPaddingLeft(); y -= textView.getTotalPaddingTop(); x += textView.getScrollX(); y += textView.getScrollY(); Layout layout = textView.getLayout(); int line = layout.getLineForVertical(y); int off = layout.getOffsetForHorizontal(line, x); ClickableSpan[] link = spannable.getSpans(off, off, ClickableSpan.class); if (link.length != 0) { if (action == MotionEvent.ACTION_UP) { link[0].onClick(textView); } else if (action == MotionEvent.ACTION_DOWN) { Selection.setSelection(spannable, spannable.getSpanStart(link[0]), spannable.getSpanEnd(link[0])); } return true; } else { Selection.removeSelection(spannable); } } return false; } } 3
 

A possible workaround for this is..

public static void makeTextViewResizable(final TextView tv, final int maxLine, final String expandText, final boolean viewMore) {      if (tv.getTag() == null) {         tv.setTag(tv.getText());     }     ViewTreeObserver vto = tv.getViewTreeObserver();     vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {          @SuppressWarnings("deprecation")         @Override         public void onGlobalLayout() {              ViewTreeObserver obs = tv.getViewTreeObserver();             obs.removeGlobalOnLayoutListener(this);             if (maxLine == 0) {                 int lineEndIndex = tv.getLayout().getLineEnd(0);                 String text = tv.getText().subSequence(0, lineEndIndex - expandText.length() + 1) + " " + expandText;                 tv.setText(text);                 tv.setMovementMethod(LinkMovementMethod.getInstance());                 tv.setText(                         addClickablePartTextViewResizable(Html.fromHtml(tv.getText().toString()), tv, maxLine, expandText,                                 viewMore), TextView.BufferType.SPANNABLE);             } else if (maxLine > 0 && tv.getLineCount() >= maxLine) {                 int lineEndIndex = tv.getLayout().getLineEnd(maxLine - 1);                 String text = tv.getText().subSequence(0, lineEndIndex - expandText.length() + 1) + " " + expandText;                 tv.setText(text);                 tv.setMovementMethod(LinkMovementMethod.getInstance());                 tv.setText(                         addClickablePartTextViewResizable(Html.fromHtml(tv.getText().toString()), tv, maxLine, expandText,                                 viewMore), TextView.BufferType.SPANNABLE);             } else {                 int lineEndIndex = tv.getLayout().getLineEnd(tv.getLayout().getLineCount() - 1);                 String text = tv.getText().subSequence(0, lineEndIndex) + " " + expandText;                 tv.setText(text);                 tv.setMovementMethod(LinkMovementMethod.getInstance());                 tv.setText(                         addClickablePartTextViewResizable(Html.fromHtml(tv.getText().toString()), tv, lineEndIndex, expandText,                                 viewMore), TextView.BufferType.SPANNABLE);             }         }     });  }  private static SpannableStringBuilder addClickablePartTextViewResizable(final Spanned strSpanned, final TextView tv,                                                                         final int maxLine, final String spanableText, final boolean viewMore) {     String str = strSpanned.toString();     SpannableStringBuilder ssb = new SpannableStringBuilder(strSpanned);      if (str.contains(spanableText)) {           ssb.setSpan(new MySpannable(false){             @Override             public void onClick(View widget) {                 if (viewMore) {                     tv.setLayoutParams(tv.getLayoutParams());                     tv.setText(tv.getTag().toString(), TextView.BufferType.SPANNABLE);                     tv.invalidate();                     makeTextViewResizable(tv, -1, "See Less", false);                 } else {                     tv.setLayoutParams(tv.getLayoutParams());                     tv.setText(tv.getTag().toString(), TextView.BufferType.SPANNABLE);                     tv.invalidate();                     makeTextViewResizable(tv, 3, ".. See More", true);                 }             }         }, str.indexOf(spanableText), str.indexOf(spanableText) + spanableText.length(), 0);      }     return ssb;  } 

Spannable Class:

import android.graphics.Color; import android.text.TextPaint; import android.text.style.ClickableSpan; import android.view.View;  public class MySpannable extends ClickableSpan {  private boolean isUnderline = true;  /**  * Constructor  */ public MySpannable(boolean isUnderline) {     this.isUnderline = isUnderline; }  @Override public void updateDrawState(TextPaint ds) {     ds.setUnderlineText(isUnderline);     ds.setColor(Color.parseColor("#1b76d3")); }  @Override public void onClick(View widget) {    } } 

Call it in this way:

myTextView.setText(discription); makeTextViewResizable(myTextView, 3, "See More", true); 
</div
 
 
0
 
vote

これは私がTextViewとAndroidを使っていれば私が持っているものです:AutoLink = "Eメール| Web" > イメージの説明を入力します。ここで

<事前> <コード> <TextView android:id="@+id/text_link" android:layout_width="0dp" android:layout_height="wrap_content" android:ellipsize="end" android:gravity="end" android:autoLink="email|web" android:maxLines="1" android:text="https://stackoverflow.com/questions/46056046/textview-maxlines-movement-method-and-ellipsize" android:textColorLink="?attr/colorAccent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@id/text_link_attribute" app:layout_constraintTop_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" />

とそのように見えます 画像の説明を入力します。ここで

カスタムテキストビュー( にインスピレーション)する場合:

<事前> <コード> class NoScrollTextView : androidx.appcompat.widget.AppCompatTextView { constructor(context: Context?) : super(context) constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super( context, attrs, defStyleAttr ) override fun scrollTo(x: Int, y: Int) { super.scrollTo(x, 0) } override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) { super.onLayout(changed, left, top, right, bottom) post { moveCursorToVisibleOffset() } } }

とそれぞれ

<事前> <コード> <com.YOU_PACKAGE_NAME_HERE.NoScrollTextView android:id="@+id/text_link" android:layout_width="0dp" android:layout_height="wrap_content" android:ellipsize="end" android:gravity="end" android:autoLink="email|web" android:singleLine="true" android:text="https://stackoverflow.com/questions/46056046/textview-maxlines-movement-method-and-ellipsize" android:textColorLink="?attr/colorAccent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@id/text_link_attribute" app:layout_constraintTop_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" />

主な機能(必要で十分な):

  1. super.scrollto(x、0)
  2. post {movecursortovisibleOffset()}
  3. Android:SingleLine = "True"
 

This is what I have if I use a TextView and android:autoLink="email|web" enter image description here

<TextView     android:id="@+id/text_link"     android:layout_width="0dp"     android:layout_height="wrap_content"     android:ellipsize="end"     android:gravity="end"     android:autoLink="email|web"     android:maxLines="1"     android:text="https://stackoverflow.com/questions/46056046/textview-maxlines-movement-method-and-ellipsize"     android:textColorLink="?attr/colorAccent"     app:layout_constraintBottom_toBottomOf="parent"     app:layout_constraintEnd_toEndOf="parent"     app:layout_constraintStart_toEndOf="@id/text_link_attribute"     app:layout_constraintTop_toBottomOf="parent"     app:layout_constraintTop_toTopOf="parent" /> 

And it looks like that enter image description here

if we use a custom TextView (inspired by):

   class NoScrollTextView : androidx.appcompat.widget.AppCompatTextView {        constructor(context: Context?) : super(context)       constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)       constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(           context,           attrs,           defStyleAttr       )        override fun scrollTo(x: Int, y: Int) {           super.scrollTo(x, 0)       }        override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {           super.onLayout(changed, left, top, right, bottom)           post { moveCursorToVisibleOffset() }       }   } 

and respectively

<com.YOU_PACKAGE_NAME_HERE.NoScrollTextView     android:id="@+id/text_link"     android:layout_width="0dp"     android:layout_height="wrap_content"     android:ellipsize="end"     android:gravity="end"     android:autoLink="email|web"     android:singleLine="true"     android:text="https://stackoverflow.com/questions/46056046/textview-maxlines-movement-method-and-ellipsize"     android:textColorLink="?attr/colorAccent"     app:layout_constraintBottom_toBottomOf="parent"     app:layout_constraintEnd_toEndOf="parent"     app:layout_constraintStart_toEndOf="@id/text_link_attribute"     app:layout_constraintTop_toBottomOf="parent"     app:layout_constraintTop_toTopOf="parent" /> 

The key features (they are necessary and sufficient):

  1. super.scrollTo(x, 0)
  2. post { moveCursorToVisibleOffset() }
  3. android:singleLine="true"
</div
 
 

関連する質問

0  TalkbackをどのようにしてAndroidを無視することができます:text要素?  ( How can i make talkback ignore androidtext element ) 
私は私のAndroidアプリで視覚障害者のためのアクセシビリティを改善しています。私のAndroidプロジェクトのpopover.xmlファイルに次のTextViewがあります。 <事前> <コード> <TextView android:layout_...

1  GridView内でImageViewにテキストを配置する方法  ( How to place text on an imageview inside a gridview ) 
ここでのチュートリアルに従った: http:// developer .android.com /リソース/チュートリアル/ views / hello-gridview.html このグリッドビューの画像の上にテキストを置く方法は疑問に思いますか?私は他...

0  動的に作成されたTextViewの高さをAndroidの50dpの高さ50dp?  ( How to make the height of a dynamically created textview exactly 50dp in android ) 
動的に作成されたTextViewの高さを50dpにすることをお勧めします。コードで<コード> dimens.xml ファイルを作成しました <事前> <コード> <dimen name="imageview_height">50dp</dimen> でを使...

28  テキストビューの隠しフィールド/タグ?  ( Hidden field tag in textview ) 
実行時に生成される値をTextViewで渡したい。テキストプロパティは他のデータに使用され、パスしたいデータは表示されません。だから、それは隠されたタグのようです。 TextViewでできることは可能ですか?もしそうなら、TextViewのどのプロパティです。...

413  Android TextViewはテキストを正当化します  ( Android textview justify text ) 
TextView のテキストを正当化します(テキストが左右の側面に面して)。 私は可能な解決策を見つけましたここにあります (垂直中心を中心_verticalなどに変更しても)機能しません。 ...

0  SQLiteデータベースからデータを引き出し、それをTextViewに表示する  ( Pull the data out of sqlite database and show it in textview ) 
こんばんは、スタックオーバーフローの親切な人々。私はデータベースからすべてのデータを引っ張り、それをTextViewに表示したいと思います。どのように私はそれを達成することができますか? さらに、データベースからデータ(すべてのもの)を引き出し、SOAPメッセ...

-2  時計TextViewのカスタムフォント  ( Custom font for clock textview ) 
私はAndroid開発の世界で新たに、私は各桁の時間が独自の書体であるように時計を作りたいです。 時間桁はそれが独自の書体であり、分桁は独自の書体を持っています。 これどうやってするの。私を助けてください。 ...

-6  TextViewの3つの値を追加し、1つのTexViewに格納する方法  ( How to add the three values of textview and to store in one texview ) 
<事前> <コード> String s1 = (getIntent().getStringExtra("Value1")); String s3 = (getIntent().getStringExtra("value2")); Str...

1  SimpleAdapterを使用してTextViewでfloat値を表示します  ( Display float value in textview using simpleadapter ) 
ProgressBar および TextView を含むカスタム ListView を使用しています。現在、進行状況データには、 9988777667 値が含まれているため、TextViewでfloat値を表示します。しかし、それはエラーを示しています:'...

107  プログラムで実行時にXMLからディメンションを使用してテキストサイズを設定する方法  ( How to set text size using dimension from xml at runtime programmatically ) 
dimens.xml、私は持っています: <事前> <コード> <dimen name="text_medium">18sp</dimen> 実行時に、この値を取得し、テキストビューのテキストサイズを設定します。 <事前> <コード> int size ...




© 2022 cndgn.com All Rights Reserved. Q&Aハウス 全著作権所有