Android自定View流式布局根据文字数量换行

所属分类: 软件编程 / Android 阅读数: 75
收藏 0 赞 0 分享

本文实例为大家分享了Android根据文字数量换行的具体代码,供大家参考,具体内容如下

//主页 定义数据框

package com.example.customwaterfallviewgroup;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
 List<String> stringList = new ArrayList<>();
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  initView();
 }

 private void initView() {
  final EditText editText = findViewById(R.id.edit);
  final CustomWaterFallViewGroup customWaterFallViewGroup = findViewById(R.id.water_fill);
  findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    //获取输入框的值
    String str = editText.getText().toString();
    //将文字放入列表
    stringList.add(str);
    //设置数据
    customWaterFallViewGroup.setData(stringList);
   }
  });
 }
}

//zhuye 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
 android:orientation="vertical"
 tools:context=".MainActivity">

 <EditText
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:id="@+id/edit"
  android:hint="输入"
  />
 <Button
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:id="@+id/button"
  android:text="add"
  />
 <com.example.customwaterfallviewgroup.CustomWaterFallViewGroup
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/water_fill"
  />
</LinearLayout>

//自定义流式布局

package com.example.customwaterfallviewgroup;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class CustomWaterFallViewGroup extends LinearLayout {
 //设置每一行最大的字符串的长度
 int mMaxSize = 22;
 //传入的字符串数组
 List<String> stringList = new ArrayList<>();
 Context mcontext;

 public CustomWaterFallViewGroup(Context context) {
  super(context);
  mcontext = context;
  init();
 }

 public CustomWaterFallViewGroup(Context context,AttributeSet attrs) {
  super(context, attrs);
  mcontext = context;
  init();
 }
 //定义布局
 private void init() {
  //设置最外层的LinearLayout 为垂直布局
  setOrientation(VERTICAL);
 }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  DisplayMetrics displayMetrics = mcontext.getResources().getDisplayMetrics();
  int widthPixels = displayMetrics.widthPixels;
  setMeasuredDimension(widthPixels,heightMeasureSpec);
 }

 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
  super.onLayout(changed, l, t, r, b);
 }

 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
 }

 public void setData(List<String> stringList) {
  //上一个输入框里的数据存到这个页面的集合中 
  this.stringList = stringList;
  showData();
 }

 private void showData() {
  //因为每一次都要重新画 ,所以移除之前的布局 显示更新过的布局
  removeAllViews();
  //优先向跟布局添加一条横向布局
  LinearLayout linearLayout_h = (LinearLayout) View.inflate(mcontext,R.layout.item_water_fall_h,null);
  addView(linearLayout_h);
  //定义临时变量。用来计算最后一行已有的字符长度
  int len = 0;
  for (int i = 0;i<stringList.size();i++){
   String str = stringList.get(i);
   //将次字符串长度与记录的已有字符串长度相加
   len += str.length();
   //-判断 如果大于最大长度,说明这一行放不下了
   //需要自动换行
   if (len > mMaxSize){
    //像跟布局添加一条横布局
    linearLayout_h = (LinearLayout) View.inflate(mcontext,R.layout.item_water_fall_h,null);
    addView(linearLayout_h);
    //换行以后因为不添加了 所以 当前的救是最后一行的长度
    len = str.length();
   }
   //添加一个textView控件
   View view = View.inflate(mcontext,R.layout.water_fall_textview,null);
   //获取到它的ID
   TextView textView = view.findViewById(R.id.water_fall_textview);
   //得到后给它赋值 (输入框里的值 给它)
   textView.setText(str);
   //添加到布局中
   linearLayout_h.addView(view);

   //设置权重 让每一行内所有的控件相加充满整行,并合理分配
   LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
   layoutParams.weight = 1;
   view.setLayoutParams(layoutParams);

   final int index = i;
   view.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
     Toast.makeText(mcontext,"您点击了"+stringList.get(index),Toast.LENGTH_SHORT).show();
    }
   });
   view.setOnLongClickListener(new OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
     stringList.remove(index);
     showData();
     return false;
    }
   });
  }
 }
}

//每一行的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/water_fall_h"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">

</LinearLayout>

//流式布局

<?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="wrap_content"
 android:orientation="horizontal"
 >

 <TextView
  android:id="@+id/water_fall_textview"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@color/colorAccent"
  android:layout_weight="1"
  android:textSize="20dp"
  android:layout_marginRight="5dp"
  android:layout_marginLeft="5dp"
  android:layout_marginTop="10dp"
  android:gravity="center"
  />
</LinearLayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多