加粗樣式
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.example.liushimodule.weight.MyFloatLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/MyFloat"
></com.example.liushimodule.weight.MyFloatLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:visibility="gone"
android:text="dcasc"
android:background="@drawable/shape"
/>
</android.support.constraint.ConstraintLayout>
MainActivity
package com.example.liushimodule;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.example.liushimodule.weight.MyFloatLayout;
public class MainActivity extends AppCompatActivity {
private String[] data = {"lvxx", "lgqflvxxlvxxlvxxlvxxlvxxlvxxlvxxlvxxlvxxlvxxlvxxlgqflvxxlvxxlvxxlvxxlvxxlvxxlvxxlvxxlvxxlvxxlvxxlvxxlvxxlvxxlvxxlvxxlvxxlvxxlvxxlvxxlvxxlvxx", "heheda", "memeda", "papapa", "lgqf", "heheda", "memeda", "papapa", "lgqf", "heheda", "memeda", "papapa", "lgqf", "heheda", "memeda", "papapa", "lgqf", "heheda", "memeda", "papapa"};
private MyFloatLayout myFloatLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myFloatLayout = findViewById(R.id.MyFloat);
myFloatLayout.setData(data);
}
}
MyFloatLayout
package com.example.liushimodule.weight;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.liushimodule.R;
public class MyFloatLayout extends LinearLayout {
private int mScreenWidth;
public MyFloatLayout(Context context, AttributeSet attrs) {
super(context, attrs);
DisplayMetrics metrics=context.getResources().getDisplayMetrics();
mScreenWidth= metrics.widthPixels;
//設置佈局顯示垂直顯示
setOrientation(VERTICAL);
}
public void setData(String[] data){
LinearLayout linearLayout=getLin();
for (int i = 0; i <data.length ; i++) {
String tmp=data[i];
int numWidth=0;
//得到一行LinearLayout到底有多少子控件 因爲要計算每個子控件加在一起的寬度
int childCount= linearLayout.getChildCount();
//這個for循環只是計算一個LinearLayout的所有的子控件的寬和高
for (int j = 0; j <childCount; j++) {
//得到每一個子控件
TextView tv = (TextView) linearLayout.getChildAt(j);
LayoutParams layoutParams = (LayoutParams) tv.getLayoutParams();
int leftMargin=layoutParams.leftMargin;
//測量這個TV的寬和高
tv.measure(getMeasuredWidth(),getMeasuredHeight());
numWidth+=tv.getMeasuredWidth()+leftMargin+tv.getPaddingLeft()+getPaddingRight();
}
TextView dataText=getText();
//設置屬性
LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.leftMargin=10;
params.topMargin=2;
dataText.setLayoutParams(params);
dataText.setText(tmp);
dataText.measure(getMeasuredWidth(),getMeasuredHeight());
int dataTextWidth=dataText.getMeasuredWidth()+dataText.getPaddingLeft()+dataText.getPaddingRight();
//考慮到一個字符串很長 就直接超過整個屏幕的長了
if(dataTextWidth>=mScreenWidth){
String s = tmp.substring(0, 4);
dataText.setText(s+"...");
dataText.measure(getMeasuredWidth(),getMeasuredHeight());
dataTextWidth=dataText.getMeasuredWidth();
}
if(mScreenWidth>=numWidth+dataTextWidth){
linearLayout.addView(dataText);
}else{
linearLayout=getLin();
linearLayout.addView(dataText);
}
}
}
//初始化子LinearLayout
private LinearLayout getLin(){
LinearLayout linearLayout=new LinearLayout(getContext());
//控制組件大小的一個工具LayoutParams
LayoutParams params=new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(params);
//this本類對象
this.addView(linearLayout);
//只要重新添加view了自動換行
return linearLayout;
}
private TextView getText(){
TextView textView=new TextView(getContext());
textView.setTextSize(20);
textView.setTextColor(Color.BLACK);
textView.setBackgroundResource(R.drawable.shape);
textView.setPadding(10,10,10,10);
return textView;
}
}