自动换行控件
Android Studio使用依赖:
compile 'cn.kevin:labellayout:1.0.0'
- 自动换行,可实现,对齐方式: 左、中、右。
自定义属性 | 含义 |
---|---|
label_gravity | 对齐方式, 居左:left,局中:center,居右:right |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<cn.kevin.labellayout.LabelLayout
android:id="@+id/label_layout"
android:layout_marginLeft="40dp"
app:label_gravity="center"
android:layout_marginRight="40dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:textSize="12sp"
android:background="@drawable/corner_shape"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="12dp"
android:paddingTop="5dp"
android:textColor="#FFFFFF"
android:paddingRight="12dp"
android:maxLines="1"
android:ellipsize="end"
android:layout_marginLeft="6dp"
android:layout_marginTop="4dp"
android:layout_marginRight="6dp"
android:layout_marginBottom="4dp"
android:paddingBottom="5dp">
</TextView>
class TextLabelAdapter extends LabelAdapter<String> {
public TextLabelAdapter(List<String> models) {
super(models);
}
@Override
public void onDataSet(View labelView, String item) {
((TextView)labelView).setText(item);
}
@Override
public View getLabelView(ViewGroup parent) {
return LayoutInflater.from(DemoActivity.this).inflate(R.layout.label, parent, false);
}
}
labelLayout = (LabelLayout) findViewById(R.id.label_layout);
List<String> list = new ArrayList<>();
list.add("升职加薪");
list.add("当上总经理");
list.add("出任ceo");
list.add("迎娶白富美");
list.add("走上人生巅峰,想想还有点小激动");
TextLabelAdapter adapter = new TextLabelAdapter(list);
labelLayout.setAdapter(adapter);