2 回答

TA貢獻1836條經驗 獲得超4個贊
如果你說的是芯片。
在 xml 布局中添加ChipGroup
您想要顯示籌碼的位置
<com.google.android.material.chip.ChipGroup
?android:id="@+id/chipGroup"
?android:layout_width="match_parent"
?app:chipSpacingVertical="2dp"
?android:layout_height="wrap_content"/>
現在您可以使用此方法添加 ArrayList,如下Chip所示ChipGroup:
private void addChip(String pItem, ChipGroup pChipGroup) {
? ? Chip lChip = new Chip(this);
? ? lChip.setText(pItem);
? ? lChip.setTextColor(getResources().getColor(R.color.primary_text));
? ? lChip.setChipBackgroundColor(getResources().getColorStateList(R.color.chip_bg));
? ? pChipGroup.addView(lChip, pChipGroup.getChildCount() - 1);
? }

TA貢獻1797條經驗 獲得超6個贊
目前,您正在更換支架的環路tags
。forEach
你只有一個持有者并不斷更新直到循環結束;這就是你最后的原因chip/tag
如果你指的是MaterialComponents的ChipGroup。
ChipGroup chipGroup = new ChipGroup(parentView.getContext());
String[] genres = {"Thriller", "Comedy", "Adventure"};
for(String genre : genres) {
? ?Chip chip = new Chip(parentView.getContext());
? ?chip.setText(genre);
? ?chipGroup.addView(chip);
}
我認為您一定正在使用,RecyclerView
因為我在您的代碼片段中看到了持有者。
以下是視圖層次結構;我假設你正在努力實現;如果沒有,那么您可以使用相同的邏輯來移動這些部分
回收視圖
標簽 - 多個標簽使用
FlowLayout
- 在視圖中添加多個標簽每個項目/視圖
您需要bind
RecyclerView 中的每個項目。
@Override
public void onBindViewHolder(final YourViewHolder holder, final int position) {
? ? ...
? ? // assuming you have FlowLayout in recyclerview view item
? ? // do add holder pattern to flowlayout as well
? ? fillAutoSpacingLayout(card.getTags(), holder.flowLayout);
}
在這里,我使用FlowLayout在 RecyclerView 的每個視圖中添加芯片或標簽。
在 onBindViewHolder 中綁定每個項目/視圖:
private void fillAutoSpacingLayout(ArrayList<String> tags, FlowLayout flowLayout) {
? ? for (String tag : tags) {
? ? ? ? TextView textView = buildLabel(tag);
? ? ? ? flowLayout.addView(textView);
? ? }
}
private TextView buildLabel(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.label_bg);
? ? return textView;
}
private float dpToPx(float dp){
? ? return TypedValue.applyDimension(
? ? ? ? ? ? TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
}
添加回答
舉報