TextView文本的显示与隐藏
2018-02-24 本文已影响131人
zhengLH
【需求】
文本展开前.png文本展开后.png
【核心代码】
mTvContentPart = findViewById(R.id.tv_content_part);
mTvContentFull = findViewById(R.id.tv_content_full);
mTvMore = findViewById(R.id.tv_more);
//介绍
String intro = "哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈";
mTvContentPart.setText(intro);
mTvContentFull.setText(intro);
mTvContentFull.setVisibility(View.GONE);
int lineCount = mTvContentPart.getLineCount();
Log.i("lee", "测量前行数1----------------------------" + lineCount); // 行数为0
// 法1:
mTvContentPart.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
:mTvContentPart.getViewTreeObserver().removeOnPreDrawListener(this);
int lineCount = mTvContentPart.getLineCount();
Log.i("lee", "测量后行数2----------------------------" + lineCount);
if (lineCount > 3){
mTvMore.setVisibility(View.VISIBLE);
}else{
mTvMore.setVisibility(View.GONE);
}
return false;
}
});
// 法2:
/* mTvContentPart.post(new Runnable() {
@Override
public void run() {
int lineCount = mTvContentPart.getLineCount();
if (lineCount > 4){
mTvMore.setVisibility(View.VISIBLE);
}else{
mTvMore.setVisibility(View.GONE);
}
}
});*/
mTvMore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!isOpen){ // 已展开
mTvContentFull.setVisibility(View.VISIBLE);
mTvContentPart.setVisibility(View.GONE);
mTvMore.setText("收起");
Drawable nav_up = getResources().getDrawable(R.mipmap.icon_hide);
nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
mTvMore.setCompoundDrawables(null, null, nav_up, null);
isOpen = true;
}else{ // 已关闭
mTvContentPart.setVisibility(View.VISIBLE);
mTvContentFull.setVisibility(View.GONE);
mTvMore.setText("展开更多");
Drawable nav_up = getResources().getDrawable(R.mipmap.icon_show_more);
nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
mTvMore.setCompoundDrawables(null, null, nav_up, null);
isOpen = false;
}
}
});
【xml布局】
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_darkorange_round"
android:orientation="vertical"
android:padding="@dimen/padding_10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XXX介绍:"
android:textColor="@color/colorWhite"
android:textSize="@dimen/textSize_18sp" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_5dp">
<TextView
android:id="@+id/tv_content_part"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lineSpacingExtra="1.5dp"
android:maxLines="4"
android:textColor="@color/colorWhite"
android:textSize="@dimen/textSize_16sp" />
<TextView
android:id="@+id/tv_content_full"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lineSpacingExtra="1.5dp"
android:textColor="@color/colorWhite"
android:textSize="@dimen/textSize_16sp" />
</FrameLayout>
<TextView
android:id="@+id/tv_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:drawablePadding="@dimen/padding_5dp"
android:drawableRight="@mipmap/icon_show_more"
android:padding="@dimen/padding_10dp"
android:text="展开更多"
android:textColor="@color/colorWhite"
android:textSize="@dimen/textSize_16sp" />
</LinearLayout>
【问题】项目中发现,如果直接通过TextView.getLineCount()方法获取行数时为0?
【原因】研究发现,setText()后立即调用getLineCount(),这时TextView还未完 成measure,要想正确的获取TextView的行数有两种方法
【答案】http://blog.csdn.net/ltym2014/article/details/52148751