实现芯片没有搜索功能

时间:2015-11-04 13:33:41

标签: android

我想实现这样的芯片: chip 但是因为我有一个预定义的阵列适配器&只需将其值放入此芯片和放大器中。我想将这些芯片设置为Textview或简单地查看。我尝试过很多芯片库但是在添加依赖项时它们都会出现某种错误。 我只需要为应用程序的外观实现芯片。没有搜索功能所以任何人都可以帮助我这个&如果您有任何更好的想法,请随时分享。

1 个答案:

答案 0 :(得分:0)

最后,我借助九张补丁图像chip.9.png&的FlowLayout:

MainActivity:

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;
    }
}}

&安培;然后在主要活动中:flowLayout.addView(llTemp)

&安培; xml文件中的流布局(mainactivity):

<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>

最终产品:final_chip_image