字数限制后加符号
1.布局文件
- 业务代码
package com.example.lenovo.mytest1708;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class FristActivity extends AppCompatActivity {
@BindView(R.id.tv_wenan_lable_name)
TextView mTvWenanLableName;
@BindView(R.id.ll_wenan_label_layout)
LinearLayout mLlLabelLayout;
@BindView(R.id.tv_wenan_add_label)
TextView mTvAddLabel;
@BindView(R.id.iv_label_right_arrow)
ImageView mIvLabelRightArrow;
@BindView(R.id.ll_wenan_label_container)
RelativeLayout mLlWenanLabelContainer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frist);
ButterKnife.bind(this);
List labelList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
labelList.add("动态添加文本textview" + i);
}
showLabelView(labelList);
}
/**
* 是否显示标签
*
* @param labelList
*/
private void showLabelView(List labelList) {
if (!isEmpty(labelList)) {
mLlLabelLayout.setVisibility(View.VISIBLE);
//小于3个标签,显示添加文本
mTvAddLabel.setVisibility(labelList.size() < 3 ? View.VISIBLE : View.GONE);
mLlLabelLayout.removeAllViews();
for (int i = 0; i < labelList.size(); i++) {
TextView labelView = new TextView(this);
//设置每个标签显示的Margins值
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, dip2px(this, 2), 0);
labelView.setLayoutParams(layoutParams);
labelView.setTextColor(ContextCompat.getColor(this, R.color.text_628BEE));
labelView.setTextSize(14);
//SingleLine 单行显示
labelView.setSingleLine(true);
labelView.setEllipsize(TextUtils.TruncateAt.END);
labelView.setId(i);
//显示文本
labelView.setText(labelList.get(i));
//textview
String symbol = "、";
TextView symbolView = new TextView(this);
symbolView.setTextColor(ContextCompat.getColor(this, R.color.text_628BEE));
symbolView.setTextSize(14);
symbolView.setText(symbol);
//限制容器的宽度
ViewGroup.LayoutParams lp = mLlLabelLayout.getLayoutParams();
lp.width = dip2px(this, 230);
mLlLabelLayout.setLayoutParams(lp);
//分别处理1个/2个/3个标签的显示状态
if (labelList.size() == 1) {
//设置最大宽度
labelView.setMaxWidth(dip2px(this, 166));
mLlLabelLayout.addView(labelView);
} else if (labelList.size() == 2) {
//只允许显示6个汉字, 超出显示...
labelView.setMaxEms(6);
if (i == 0) {
mLlLabelLayout.addView(labelView, 0);
mLlLabelLayout.addView(symbolView, 1);
} else if (i == 1) {
mLlLabelLayout.addView(labelView, 2);
}
} else {
//只允许显示4个汉字, 超出显示...
labelView.setMaxEms(4);
if (i == 0) {
mLlLabelLayout.addView(labelView, 0);
mLlLabelLayout.addView(symbolView, 1);
} else if (i == 1) {
mLlLabelLayout.addView(labelView, 2);
mLlLabelLayout.addView(symbolView, 3);
} else if (i == 2) {
mLlLabelLayout.addView(labelView, 4);
}
}
}
} else {
mLlLabelLayout.removeAllViews();
mTvAddLabel.setVisibility(View.VISIBLE);
mLlLabelLayout.setVisibility(View.GONE);
}
}
public int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
public boolean isEmpty(Collection c) {
return (c == null || c.size() == 0);
}
}
效果1
仿微博标签样式
- 布局文件
2.style样式
3.业务代码
package com.example.lenovo.mytest1708;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.lenovo.mytest1708.view.AutoNewLineLayout;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class FristActivity extends AppCompatActivity {
@BindView(R.id.tv_wenan_lable_name)
TextView mTvWenanLableName;
@BindView(R.id.ll_wenan_label_layout)
AutoNewLineLayout mLlLabelLayout;
@BindView(R.id.ll_wenan_label_container)
RelativeLayout mLlWenanLabelContainer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frist);
ButterKnife.bind(this);
List labelList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
labelList.add("微博样式文本text" + i);
}
showLabelView(labelList);
}
/**
* 是否显示标签
*
* @param labelList
*/
private void showLabelView(List labelList) {
if (!isEmpty(labelList)) {
mLlLabelLayout.setVisibility(View.VISIBLE);
mLlLabelLayout.removeAllViews();
for (int i = 0; i < labelList.size(); i++) {
TextView labelView = new TextView(this);
//设置每个标签显示的Margins值
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, dip2px(this, 2), 0);
labelView.setLayoutParams(layoutParams);
labelView.setTextColor(ContextCompat.getColor(this, R.color.text_628BEE));
labelView.setTextSize(14);
//SingleLine 单行显示
labelView.setSingleLine(true);
labelView.setEllipsize(TextUtils.TruncateAt.END);
labelView.setId(i);
//标签点击事件
final int finalI = i;
labelView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(FristActivity.this, "点击" + finalI, Toast.LENGTH_SHORT).show();
}
});
//最多显示10个汉字
labelView.setEms(10);
//显示文本
labelView.setText("#" + labelList.get(i) + "#");
mLlLabelLayout.addView(labelView);
}
} else {
mLlLabelLayout.removeAllViews();
mLlLabelLayout.setVisibility(View.GONE);
}
}
public int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
public boolean isEmpty(Collection c) {
return (c == null || c.size() == 0);
}
}