最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2020年09月23日 07:55:12   作者:被閏土扎住的猹  
這篇文章主要為大家詳細介紹了Android自定View流式布局,根據(jù)文字數(shù)量換行,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android根據(jù)文字數(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;
 //傳入的字符串數(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) {
  //上一個輸入框里的數(shù)據(jù)存到這個頁面的集合中 
  this.stringList = stringList;
  showData();
 }

 private void showData() {
  //因為每一次都要重新畫 ,所以移除之前的布局 顯示更新過的布局
  removeAllViews();
  //優(yōu)先向跟布局添加一條橫向布局
  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);

   //設(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,"您點擊了"+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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • android實現(xiàn)通過NFC讀取卡號

    android實現(xiàn)通過NFC讀取卡號

    這篇文章主要介紹了android實現(xiàn)通過NFC讀取卡號,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Android刮刮樂效果-proterDuffXfermode的示例代碼

    Android刮刮樂效果-proterDuffXfermode的示例代碼

    這篇文章主要介紹了Android刮刮樂效果-proterDuffXfermode,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • Android使用Activity實現(xiàn)簡單的可輸入對話框

    Android使用Activity實現(xiàn)簡單的可輸入對話框

    大家在做彈出對話框效果的時候最容易想到的是用Dialog顯示,但其實彈出對話框的實現(xiàn)效果有兩種:Dialog和Activity,那么下面這篇文章就來給大家介紹了關(guān)于Android使用Activity如何實現(xiàn)一個簡單的可輸入對話框的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-10-10
  • 利用Kotlin的協(xié)程實現(xiàn)簡單的異步加載詳解

    利用Kotlin的協(xié)程實現(xiàn)簡單的異步加載詳解

    這篇文章主要給大家介紹了關(guān)于利用Kotlin的協(xié)程實現(xiàn)簡單的異步加載的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-03-03
  • Android實現(xiàn)Service重啟的方法

    Android實現(xiàn)Service重啟的方法

    這篇文章主要介紹了Android實現(xiàn)Service重啟的方法,涉及Android操作Service組件實現(xiàn)服務(wù)重啟的功能,需要的朋友可以參考下
    2015-05-05
  • Android頂部工具欄和底部工具欄的簡單實現(xiàn)代碼

    Android頂部工具欄和底部工具欄的簡單實現(xiàn)代碼

    Android頂部工具欄和底部工具欄的簡單實現(xiàn)代碼,需要的朋友可以參考一下
    2013-05-05
  • Android自定義ActionBar實例

    Android自定義ActionBar實例

    這篇文章主要介紹了Android自定義ActionBar的實現(xiàn)方法,以實例形式較為詳細的分析了自定義ActionBar的原理與具體步驟,并附帶完整實例代碼供大家下載查看,需要的朋友可以參考下
    2014-11-11
  • Android自定義控件RatingBar調(diào)整字體大小

    Android自定義控件RatingBar調(diào)整字體大小

    這篇文章主要為大家詳細介紹了Android自定義控件RatingBar調(diào)整字體大小的相關(guān)資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Android編程使用加速度傳感器實現(xiàn)搖一搖功能及優(yōu)化的方法詳解

    Android編程使用加速度傳感器實現(xiàn)搖一搖功能及優(yōu)化的方法詳解

    這篇文章主要介紹了Android編程使用加速度傳感器實現(xiàn)搖一搖功能及優(yōu)化的方法,結(jié)合實例形式分析了Android傳感器的調(diào)用方法、參數(shù)含義及具體使用技巧,需要的朋友可以參考下
    2017-08-08
  • Android開發(fā)EditText禁止輸入監(jiān)聽及InputFilter字符過濾

    Android開發(fā)EditText禁止輸入監(jiān)聽及InputFilter字符過濾

    這篇文章主要為大家介紹了Android開發(fā)EditText禁止輸入監(jiān)聽及InputFilter字符過濾示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06

最新評論

永宁县| 得荣县| 福州市| 隆德县| 八宿县| 西和县| 交城县| 黄石市| 阜城县| 大埔县| 东阿县| 同江市| 盖州市| 巧家县| 淮阳县| 新兴县| 巩义市| 科技| 晋中市| 清丰县| 天津市| 科技| 清丰县| 广东省| 卢湾区| 铜鼓县| 双桥区| 沁水县| 方正县| 甘南县| 沈阳市| 讷河市| 吉木乃县| 教育| 克拉玛依市| 海林市| 喀喇沁旗| 长垣县| 于都县| 孙吴县| 罗平县|