Android 实现没有搜索功能的芯片
我想实现这样的芯片: 但是因为我有一个预定义的数组适配器&只需要将它的值放入这个芯片中&我想将这些芯片设置为文本视图或简单视图。我尝试了许多芯片库,但在添加依赖项时,它们都出现了某种错误。Android 实现没有搜索功能的芯片,android,Android,我想实现这样的芯片: 但是因为我有一个预定义的数组适配器&只需要将它的值放入这个芯片中&我想将这些芯片设置为文本视图或简单视图。我尝试了许多芯片库,但在添加依赖项时,它们都出现了某种错误。 我只需要为应用程序的外观而不是搜索功能安装芯片,那么有人能在这方面帮助我吗?如果你有更好的想法,请随时与我分享。最后,我在九块补丁图像和流程布局的帮助下做到了这一点: 主要活动: LinearLayout llTemp = new LinearLayout(this); llT
我只需要为应用程序的外观而不是搜索功能安装芯片,那么有人能在这方面帮助我吗?如果你有更好的想法,请随时与我分享。最后,我在九块补丁图像和流程布局的帮助下做到了这一点: 主要活动:
LinearLayout llTemp = new LinearLayout(this);
llTemp.setOrientation(LinearLayout.HORIZONTAL);
llTemp.setBackgroundResource(R.drawable.chip);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
llTemp.setLayoutParams(params);
llTemp.setGravity(Gravity.CENTER_VERTICAL);
TextView tv = new TextView(this);
tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
tv.setPadding(0, 0, 5, 0);
tv.setText("Some text");
CircularImageView iv = new CircularImageView(this);
LinearLayout.LayoutParams ivParams = new LinearLayout.LayoutParams(50, 50);
ivParams.setMargins(0, 0, 5, 0);
iv.setLayoutParams(ivParams);
iv.setBorderWidth(0);
Picasso.with(this).load(IMAGE_LINK + IMAGE_FILE).into(iv);
ImageView ivCloseBtn = new ImageView(this);
ivCloseBtn.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
ivCloseBtn.setPadding(2, 2, 2, 2);
ivCloseBtn.setBackgroundResource(R.drawable.icon_close);
FlowLayout.java:
public class FlowLayout extends ViewGroup {
private int paddingHorizontal;
private int paddingVertical;
public FlowLayout(Context context) {
super(context);
init();
}
public FlowLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public FlowLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
paddingHorizontal = getResources().getDimensionPixelSize(R.dimen.flowlayout_horizontal_padding);
paddingVertical = getResources().getDimensionPixelSize(R.dimen.flowlayout_vertical_padding);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int childLeft = getPaddingLeft();
int childTop = getPaddingTop();
int lineHeight = 0;
// 100 is a dummy number, widthMeasureSpec should always be EXACTLY for FlowLayout
int myWidth = resolveSize(100, widthMeasureSpec);
int wantedHeight = 0;
for (int i = 0; i < getChildCount(); i++) {
final View child = getChildAt(i);
if (child.getVisibility() == View.GONE) {
continue;
}
// let the child measure itself
child.measure(
getChildMeasureSpec(widthMeasureSpec, 0, child.getLayoutParams().width),
getChildMeasureSpec(heightMeasureSpec, 0, child.getLayoutParams().height));
int childWidth = child.getMeasuredWidth();
int childHeight = child.getMeasuredHeight();
// lineheight is the height of current line, should be the height of the highest view
lineHeight = Math.max(childHeight, lineHeight);
if (childWidth + childLeft + getPaddingRight() > myWidth) {
// wrap this line
childLeft = getPaddingLeft();
childTop += paddingVertical + lineHeight;
lineHeight = childHeight;
}
childLeft += childWidth + paddingHorizontal;
}
wantedHeight += childTop + lineHeight + getPaddingBottom();
setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec));
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
int childLeft = getPaddingLeft();
int childTop = getPaddingTop();
int lineHeight = 0;
int myWidth = right - left;
for (int i = 0; i < getChildCount(); i++) {
final View child = getChildAt(i);
if (child.getVisibility() == View.GONE) {
continue;
}
int childWidth = child.getMeasuredWidth();
int childHeight = child.getMeasuredHeight();
lineHeight = Math.max(childHeight, lineHeight);
if (childWidth + childLeft + getPaddingRight() > myWidth) {
childLeft = getPaddingLeft();
childTop += paddingVertical + lineHeight;
lineHeight = childHeight;
}
child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
childLeft += childWidth + paddingHorizontal;
}
}}
最终产品:Hello@ritz,根据您对Stackoverflow的看法,我断定您是新来的。你写的问题不是stackoverflow是如何工作的。请阅读关于stackoverflow的“如何提问”:我们很乐意在某个编程问题上为您提供帮助。欢迎访问SO。你的问题离题了,因为它要求我们做你的工作,而没有显示你做了什么。我知道如何回答你的问题,但是这会激励其他用户发布这样的问题。我不想让任何人为我发布代码,但只是一些说明来帮助我,因为我已经尝试了各种芯片库,但对我不起作用&加上芯片基本上与autocompletetextview一起使用,所以你可以使用它来实现更好的搜索功能……我会的很高兴有人能指导我在视图中简单地显示类似芯片的东西。就这样对不起,我是Stackoverflow的新手,所以我的提问方式有点不成熟。非常感谢@FrankN.Stein。你刚刚帮了我的忙。
<ScrollView android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<com.example.FlowLayout
android:id="@+id/fl"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>