7
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Androidのテキスト省略表示について(Ellipsize)

Posted at

#テキスト省略表示やりかた
たぶん5.0以上じゃないと複数行にバグがあったと思う前提で。

##ソース

        mBodyText = (TextView)findViewById(R.id.text_body);
        //ViewTreeObserverでOnPreDraw捕まる。
        mBodyText.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                int lineCount = mBodyText.getLineCount();
                //省略された行数を求める
                int hiddenLineCount = mBodyText.getLayout().getEllipsisCount(lineCount-1);
                if(hiddenLineCount>0){
                    mMottoMiru.setVisibility(View.VISIBLE);
                }else{
                    mMottoMiru.setVisibility(View.GONE);
                }

                return true;
            }
        });
        mMottoMiru = (TextView)findViewById(R.id.mottomiru);

        mMottoMiru.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBodyText.setMaxLines(Integer.MAX_VALUE);
                //setEllipsize null 忘れずに
                mBodyText.setEllipsize(null);
            }

        });

##レイアウト


    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <LinearLayout
            android:id="@+id/text_erea"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:id="@+id/text_body"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:ellipsize="end"
                android:maxLines="2"
                android:text="@string/large_text" />
            <TextView
                android:id="@+id/mottomiru"
                android:layout_gravity="right"
                android:textColor="#5555ff"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="もっとみる"/>
            <TextView
                android:id="@+id/text_body2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:ellipsize="end"
                android:maxLines="2"
                android:text="IGMA 14mm F1.8 DG HSM | Art は、異次元の視覚体験を可能にする真のハイスピードレンズです。" />
            <TextView
                android:id="@+id/mottomiru2"
                android:layout_gravity="right"
                android:textColor="#5555ff"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="もっとみる"/>
        </LinearLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="#000000"
            android:layout_below="@id/text_erea"
            android:layout_marginTop="10dp"
            ></RelativeLayout>



    </RelativeLayout>

##SS

上のFoveonセンサーの説明は2行を超えているので省略され。もっとみるもでる。

7
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?