动态添加textview, 实现字数限制后加符号, 以及仿微博标
2017-08-17 本文已影响0人
追逐未来2016
字数限制后加符号
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="com.example.lenovo.mytest1708.FristActivity">
<RelativeLayout
android:id="@+id/ll_wenan_label_container"
android:layout_width="match_parent"
android:layout_height="49dp"
android:background="@android:color/white">
<TextView
android:id="@+id/tv_wenan_lable_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:text="参与话题"
android:textColor="@color/text_212121"
android:textSize="14sp"/>
<!--标签容器-->
<LinearLayout
android:id="@+id/ll_wenan_label_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="9dp"
android:gravity="right"
android:layout_toLeftOf="@+id/tv_wenan_add_label"
android:orientation="horizontal"/>
<TextView
android:id="@+id/tv_wenan_add_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="7dp"
android:layout_toLeftOf="@+id/iv_label_right_arrow"
android:text="添加"
android:textColor="@color/text_666666"
android:textSize="14sp"/>
<ImageView
android:id="@+id/iv_label_right_arrow"
android:layout_width="6dp"
android:layout_height="12dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="11dp"
android:background="@drawable/wenan_right_arrow_icon"/>
</RelativeLayout>
</LinearLayout>
- 业务代码
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<String> labelList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
labelList.add("动态添加文本textview" + i);
}
showLabelView(labelList);
}
/**
* 是否显示标签
*
* @param labelList
*/
private void showLabelView(List<String> 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 <V> boolean isEmpty(Collection<V> c) {
return (c == null || c.size() == 0);
}
}
效果1
textview01.png仿微博标签样式
- 布局文件
<?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="com.example.lenovo.mytest1708.FristActivity">
<RelativeLayout
android:layout_marginTop="10dp"
android:id="@+id/ll_wenan_label_container"
android:layout_width="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_height="wrap_content"
android:background="@android:color/white">
<TextView
android:id="@+id/tv_wenan_lable_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="内容"
android:textColor="@color/text_212121"
android:textSize="18sp"/>
<!--标签容器-->
<com.example.lenovo.mytest1708.view.AutoNewLineLayout
android:id="@+id/ll_wenan_label_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="right"
android:layout_alignParentRight="true"
android:orientation="horizontal"/>
</RelativeLayout>
</LinearLayout>
2.style样式
<declare-styleable name="AutoNewLineLayout">
<attr name="horizontalSpace" format="dimension|reference"/>
<attr name="vertivalSpace" format="dimension|reference"/>
</declare-styleable>
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<String> labelList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
labelList.add("微博样式文本text" + i);
}
showLabelView(labelList);
}
/**
* 是否显示标签
*
* @param labelList
*/
private void showLabelView(List<String> 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 <V> boolean isEmpty(Collection<V> c) {
return (c == null || c.size() == 0);
}
}