首先效果图
收起
展开
代码
xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="@color/colorPrimary"
android:gravity="center"
android:text="TextView最多显示几行"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp">
<TextView
android:id="@+id/activity_textviewmax_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|left"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@+id/activity_textviewmax_imageview"
android:maxLines="3" />
<ImageView
android:id="@+id/activity_textviewmax_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@mipmap/up" />
</RelativeLayout>
</LinearLayout>
java 代码
package com.wjn.viewlistdemo.activity.textview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.wjn.viewlistdemo.R;
import com.wjn.viewlistdemo.utils.StatusBarUtil;
public class TextViewMaxActivity extends AppCompatActivity {
private TextView textView;
private ImageView imageView;
private boolean isimageview=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_textviewmax);
//根据状态栏颜色来决定 状态栏背景 用黑色还是白色 true:是否修改状态栏字体颜色
StatusBarUtil.setStatusBarMode(this, false, false, R.color.colorPrimary);
initView();
}
/**
* 初始化各种View
* */
private void initView(){
textView=findViewById(R.id.activity_textviewmax_textview);
imageView=findViewById(R.id.activity_textviewmax_imageview);
textView.setText(StringUtils.string1);
// textView.setText(StringUtils.string2);
textView.post(new Runnable() {
@Override
public void run() {
boolean b=isTextView(textView);
if(b){
imageView.setVisibility(View.VISIBLE);
}else{
imageView.setVisibility(View.GONE);
}
}
});
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!isimageview){
imageView.setBackgroundResource(R.mipmap.downs);
textView.setMaxLines(10000);
isimageview=true;
}else{
imageView.setBackgroundResource(R.mipmap.up);
textView.setMaxLines(3);
isimageview=false;
}
}
});
}
private boolean isTextView(TextView textView){
float m=textView.getPaint().measureText(textView.getText().toString());
float n=3*(textView.getWidth()-textView.getPaddingRight()-textView.getPaddingLeft());
return m>n;
}
}
由于TextView渲染是需要时间的,如果在获取到TextView并setText之后立马进行判断,会发现此时TextView还没有绘制出来,拿到的属性均为初始值,比如前面函数中调用的getWidth返回为0,这样显然是不对的。所以要在TextView.post方法中执行。