先給大家看效果圖:
歷史記錄本地保存到share裏面,在onStart裏面獲取調用數據,點擊搜索的時候添加進share。
導入依賴:
compile 'com.nex3z:flow-layout:1.0.0'
https://github.com/nex3z/FlowLayoutUsage:
<com.nex3z.flowlayout.FlowLayout android:id="@+id/flow" android:layout_width="match_parent" android:layout_height="wrap_content" app:flChildSpacing="7dp" app:flRowSpacing="8dp" />
屬性說明:
這是github上的屬性介紹,看不懂的可以略過,我後面有中文翻譯,以及效果展示。
Attribute | Format | Description |
---|---|---|
flFlow | boolean | true to allow flow. false to restrict all child views in one row. The default is
true . |
flChildSpacing | auto /dimension |
The horizontal spacing between child views. Either auto , or a fixed size. The default is 0dp. |
flChildSpacingForLastRow | auto /align /dimension |
The horizontal spacing between child views of the last row. Either auto ,
align or a fixed size. If not set, childSpacing will be used instead. |
flRowSpacing | auto /dimension |
The vertical spacing between rows. Either auto , or a fixed size. The default is 0dp. |
flRtl | boolean | true to layout child views from right to left. false to layout from left to right. The default is
false . |
flMaxRows | integer | The maximum height of FlowLayout in terms of number of rows. |
中文:
FlowLayout flowLayout = (FlowLayout) findViewById(R.id.flow);
for (int i = 0; i < strList.size(); i++) { TextView textView = buildLabel(strList.get(i)); flowLayout.addView(textView); }flowlayout就是一個佈局,像這樣添加textView進去,就初始化好了。
屬性:
//默認爲true,flase只顯示一行
flowLayout.setFlow(false); 如圖所示:
//這個三個屬性都是間距,橫縱的距離
flowLayout.setChildSpacing(8); flowLayout.setRowSpacing(8); flowLayout.setChildSpacingForLastRow(8);
//這個屬性默認是false,爲true排列方向從右到左,上效果圖。
app:flRtl="true"這個屬性我沒有在代碼裏找到,只能在xml裏面設置
//最大行數,如果設置爲2那麼顯示出的行數只有兩行。
flowLayout.setMaxRows(2); 如之前的圖片,我本來有四行,設置以後只顯示兩行。
我使用這個佈局用到的代碼如下:
for (int i = 0; i < strList.size(); i++) { TextView textView = buildLabel(strList.get(i)); flowLayout.addView(textView); }
private TextView buildLabel(final String text) { TextView textView = new TextView(this); textView.setText(text); textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16); textView.setPadding((int) dpToPx(16), (int) dpToPx(8), (int) dpToPx(16), (int) dpToPx(8)); textView.setBackgroundResource(R.drawable.bg_gray); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { et.setText(text); } }); return textView; } private float dpToPx(float dp) { return TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics()); }
需要的代碼都在這裏了。
此博客方便自己使用與他人交流,未經同意不允許他人轉載。