我在安排文本视图方面面临问题,我在线性布局水平视图中动态创建芯片,当我创建多个芯片时,只有少数芯片正在查看其余的被隐藏,如何在第二个芯片中排列芯片当第一行被填满时排。请帮我解决这个问题。
以下代码用于以线性布局动态创建芯片
LinearLayout linearChip = (LinearLayout)findViewById(R.id.linear_chip);
Chip chip = new Chip(this);
chip.setChipText(tag);
chip.setClosable(true);
chip.setPadding(10,0,0,0);
chip.setTextColor(Color.BLACK);
chip.setBackgroundColor(Color.LTGRAY);
chip.setCloseColor(Color.BLACK);
chip.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
chip.setOnCloseClickListener(new OnCloseClickListener() {
@Override
public void onCloseClick(View v) {
linearChip.removeView((View) v.getParent());
}
});
linearChip.addView(chip);
}
答案 0 :(得分:1)
请检查此FlowLayout Library 。
FlowLayout在添加视图时按预期工作,如果在FlowLayout中添加了视图,则会自动调整视图。
<com.FlowLayout
android:id="@+id/chips_box_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start"/>
FlowLayout chipsBoxLayout = (FlowLayout)findViewById(R.id.chips_box_layout);
FlowLayout.LayoutParams params = new FlowLayout.LayoutParams(FlowLayout.LayoutParams.WRAP_CONTENT, FlowLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);
String demoText;
for(int i=0; i< 5; i++){
demoText = "hello" + i;
TextView t = new TextView(context);
t.setLayoutParams(params);
t.setPadding(5, 5, 5, 5);
t.setText(demoText);
t.setTextColor(Color.WHITE);
t.setBackgroundColor(Color.BLUE);
chipsBoxLayout.addView(t);
}