a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統(tǒng) > Android > 正文

Android自定View流式布局根據(jù)文字?jǐn)?shù)量換行

2019-10-21 21:34:19
字體:
供稿:網(wǎng)友

本文實(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();      //-判斷 如果大于最大長度,說明這一行放不下了      //需要自動換行      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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 亚洲精品久久久日韩美女极品合集下载 | 成人福利在线观看 | 国产色播| 青青草狠狠操 | 色婷婷在线视频 | 妞干网国产 | 亚洲一区二区精品视频 | 免费在线一区二区三区 | 成人做爰www免费看视频网站 | 一区二区三区四区精品 | 亚洲一区二区av | 精品国产一区二区三区久久久蜜月 | 午夜精品久久久久久久久久久久 | yy6080久久伦理一区二区 | 久久国产精彩视频 | 色婷婷综合久久久久中文一区二 | 黄a免费 | 毛片入口 | 999这里只有精品 | 亚洲精品视频网 | 国产www在线| 天天拍天天操 | 日韩视频在线观看 | 欧美精品网站 | 久久精品99| 国产高清自拍 | 中文字幕在线一区 | 黄色毛片在线看 | 久久九 | 一本久久a久久精品亚洲 | 四虎成人在线播放 | 一区二区三区免费看 | 久久国 | 久久精品国产99国产 | 成人亚洲一区二区 | 成人av电影免费在线观看 | 欧美精品久久久久久精华液 | 黄色一级毛片在线观看 | 久久1区| 女人色偷偷aa久久天堂 | 久久99精品国产麻豆婷婷洗澡 |