Android自定View流式布局根據(jù)文字?jǐn)?shù)量換行
本文實(shí)例為大家分享了Android根據(jù)文字?jǐn)?shù)量換行的具體代碼,供大家參考,具體內(nèi)容如下
//主頁 定義數(shù)據(jù)框
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); //設(shè)置數(shù)據(jù) 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 { //設(shè)置每一行最大的字符串的長度 int mMaxSize = 22; //傳入的字符串?dāng)?shù)組 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() { //設(shè)置最外層的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) { //上一個(gè)輸入框里的數(shù)據(jù)存到這個(gè)頁面的集合中 this.stringList = stringList; showData(); } private void showData() { //因?yàn)槊恳淮味家匦庐?,所以移除之前的布局 顯示更新過的布局 removeAllViews(); //優(yōu)先向跟布局添加一條橫向布局 LinearLayout linearLayout_h = (LinearLayout) View.inflate(mcontext,R.layout.item_water_fall_h,null); addView(linearLayout_h); //定義臨時(shí)變量。用來計(jì)算最后一行已有的字符長度 int len = 0; for (int i = 0;i<stringList.size();i++){ String str = stringList.get(i); //將次字符串長度與記錄的已有字符串長度相加 len += str.length(); //-判斷 如果大于最大長度,說明這一行放不下了 //需要自動(dòng)換行 if (len > mMaxSize){ //像跟布局添加一條橫布局 linearLayout_h = (LinearLayout) View.inflate(mcontext,R.layout.item_water_fall_h,null); addView(linearLayout_h); //換行以后因?yàn)椴惶砑恿?所以 當(dāng)前的救是最后一行的長度 len = str.length(); } //添加一個(gè)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); //設(shè)置權(quán)重 讓每一行內(nèi)所有的控件相加充滿整行,并合理分配 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,"您點(diǎn)擊了"+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>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android中使用WebSocket實(shí)現(xiàn)群聊和消息推送功能(不使用WebView)
WebSocket protocol 是HTML5一種新的協(xié)議。它實(shí)現(xiàn)了瀏覽器與服務(wù)器全雙工通信(full-duplex)。本文給大家介紹Android中使用WebSocket實(shí)現(xiàn)群聊和消息推送功能(不使用WebView),需要的朋友參考下2016-02-02Android實(shí)現(xiàn)簡單斷點(diǎn)續(xù)傳和下載到本地功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡單斷點(diǎn)續(xù)傳和下載到本地功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Android快速實(shí)現(xiàn)斷點(diǎn)續(xù)傳的方法
這篇文章主要為大家詳細(xì)介紹了Android快速實(shí)現(xiàn)斷點(diǎn)續(xù)傳的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Android常用定時(shí)器的實(shí)現(xiàn)方式
我們?cè)陂_發(fā)中時(shí)常需要寫一些定時(shí)的任務(wù),比如每5秒執(zhí)行一次,下面這篇文章主要給大家介紹了關(guān)于Android常用定時(shí)器的實(shí)現(xiàn)方式,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09Android中AutoCompleteTextView自動(dòng)提示
這篇文章主要為大家詳細(xì)介紹了Android中AutoCompleteTextView自動(dòng)提示的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12Android開源項(xiàng)目PullToRefresh下拉刷新功能詳解
這篇文章主要為大家詳細(xì)介紹了Android開源項(xiàng)目PullToRefresh下拉刷新功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09Flutter狀態(tài)管理Provider的使用示例詳解
這篇文章主要為大家介紹了Flutter狀態(tài)管理Provider的使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Android Handler消息派發(fā)機(jī)制源碼分析
這篇文章主要為大家詳細(xì)分析了Android Handler消息派發(fā)機(jī)制源碼,感興趣的小伙伴們可以參考一下2016-07-07Android實(shí)現(xiàn)創(chuàng)建或升級(jí)數(shù)據(jù)庫時(shí)執(zhí)行語句
這篇文章主要介紹了Android實(shí)現(xiàn)創(chuàng)建或升級(jí)數(shù)據(jù)庫時(shí)執(zhí)行語句,是比較實(shí)用的功能,需要的朋友可以參考下2014-08-08