使2个文本视图一个接一个地保持 - Android

时间:2014-08-15 03:32:38

标签: android

我想要2个textviews来保持这样的

textview1 textview1 textview1 textview1 textview1 textview1 textview1 textview1 textview1 textview1 textview1 textview1 textview1 textview1|textView2 textView2 textView2 textView2

textview1的长度可能超过1行,textview2必须在textview1与textview1结束的同一行之后保持正确。有什么想法吗?

3 个答案:

答案 0 :(得分:1)

你可以使用课程

public class PredicateLayout extends ViewGroup {

public static final int DEFAULT_HORIZONTAL_SPACING = 0;
public static final int DEFAULT_VERTICAL_SPACING = 0;
private final int horizontalSpacing;
private final int verticalSpacing;
private List<RowMeasurement> currentRows = Collections.emptyList();

public PredicateLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.RowLayout);
    horizontalSpacing = styledAttributes.getDimensionPixelSize(R.styleable.RowLayout_android_horizontalSpacing,
            DEFAULT_HORIZONTAL_SPACING);
    verticalSpacing = styledAttributes.getDimensionPixelSize(R.styleable.RowLayout_android_verticalSpacing,
            DEFAULT_VERTICAL_SPACING);
    styledAttributes.recycle();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    final int maxInternalWidth = MeasureSpec.getSize(widthMeasureSpec) - getHorizontalPadding();
    final int maxInternalHeight = MeasureSpec.getSize(heightMeasureSpec) - getVerticalPadding();
    List<RowMeasurement> rows = new ArrayList<RowMeasurement>();
    RowMeasurement currentRow = new RowMeasurement(maxInternalWidth, widthMode);
    rows.add(currentRow);
    for (View child : getLayoutChildren()) {
        LayoutParams childLayoutParams = child.getLayoutParams();
        int childWidthSpec = createChildMeasureSpec(childLayoutParams.width, maxInternalWidth, widthMode);
        int childHeightSpec = createChildMeasureSpec(childLayoutParams.height, maxInternalHeight, heightMode);
        child.measure(childWidthSpec, childHeightSpec);
        int childWidth = child.getMeasuredWidth();
        int childHeight = child.getMeasuredHeight();
        if (currentRow.wouldExceedMax(childWidth)) {
            currentRow = new RowMeasurement(maxInternalWidth, widthMode);
            rows.add(currentRow);
        }
        currentRow.addChildDimensions(childWidth, childHeight);
    }

    int longestRowWidth = 0;
    int totalRowHeight = 0;
    for (int index = 0; index < rows.size(); index++) {
        RowMeasurement row = rows.get(index);
        totalRowHeight += row.getHeight();
        if (index < rows.size() - 1) {
            totalRowHeight += verticalSpacing;
        }
        longestRowWidth = Math.max(longestRowWidth, row.getWidth());
    }
    setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? MeasureSpec.getSize(widthMeasureSpec) : longestRowWidth
            + getHorizontalPadding(), heightMode == MeasureSpec.EXACTLY ? MeasureSpec.getSize(heightMeasureSpec)
            : totalRowHeight + getVerticalPadding());
    currentRows = Collections.unmodifiableList(rows);
}

private int createChildMeasureSpec(int childLayoutParam, int max, int parentMode) {
    int spec;
    if (childLayoutParam == LayoutParams.MATCH_PARENT) {
        spec = MeasureSpec.makeMeasureSpec(max, MeasureSpec.EXACTLY);
    } else if (childLayoutParam == LayoutParams.WRAP_CONTENT) {
        spec = MeasureSpec.makeMeasureSpec(max, parentMode == MeasureSpec.UNSPECIFIED ? MeasureSpec.UNSPECIFIED
                : MeasureSpec.AT_MOST);
    } else {
        spec = MeasureSpec.makeMeasureSpec(childLayoutParam, MeasureSpec.EXACTLY);
    }
    return spec;
}

@Override
protected void onLayout(boolean changed, int leftPosition, int topPosition, int rightPosition, int bottomPosition) {
    final int widthOffset = getMeasuredWidth() - getPaddingRight();
    int x = getPaddingLeft();
    int y = getPaddingTop();

    Iterator<RowMeasurement> rowIterator = currentRows.iterator();
    RowMeasurement currentRow = rowIterator.next();
    for (View child : getLayoutChildren()) {
        final int childWidth = child.getMeasuredWidth();
        final int childHeight = child.getMeasuredHeight();
        if (x + childWidth > widthOffset) {
            x = getPaddingLeft();
            y += currentRow.height + verticalSpacing;
            if (rowIterator.hasNext()) {
                currentRow = rowIterator.next();
            }
        }
        child.layout(x, y, x + childWidth, y + childHeight);
        x += childWidth + horizontalSpacing;
    }
}

private List<View> getLayoutChildren() {
    List<View> children = new ArrayList<View>();
    for (int index = 0; index < getChildCount(); index++) {
        View child = getChildAt(index);
        if (child.getVisibility() != View.GONE) {
            children.add(child);
        }
    }
    return children;
}

protected int getVerticalPadding() {
    return getPaddingTop() + getPaddingBottom();
}

protected int getHorizontalPadding() {
    return getPaddingLeft() + getPaddingRight();
}

private final class RowMeasurement {
    private final int maxWidth;
    private final int widthMode;
    private int width;
    private int height;

    public RowMeasurement(int maxWidth, int widthMode) {
        this.maxWidth = maxWidth;
        this.widthMode = widthMode;
    }

    public int getHeight() {
        return height;
    }

    public int getWidth() {
        return width;
    }

    public boolean wouldExceedMax(int childWidth) {
        return widthMode == MeasureSpec.UNSPECIFIED ? false : getNewWidth(childWidth) > maxWidth;
    }

    public void addChildDimensions(int childWidth, int childHeight) {
        width = getNewWidth(childWidth);
        height = Math.max(height, childHeight);
    }

    private int getNewWidth(int childWidth) {
        return width == 0 ? childWidth : width + horizontalSpacing + childWidth;
    }
}

}

的xml:

<com.yourpackage.PredicateLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

       <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView1" />
       <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView1" />
       <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView2" />

    </com.yourpackage.PredicateLayout>

修改 你需要在/res/values/attrs.xml下创建xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="RowLayout">
    <attr name="android:verticalSpacing" />
    <attr name="android:horizontalSpacing" />
</declare-styleable>
</resources>

答案 1 :(得分:0)

我知道的三种方式,可能存在更好的方式:

1)在Html中加载文本并将其设置为文本视图(您可以使用html样式和...)

String text = "<font color=#cc0029>"+text1+"</font> <font color=#ffcc00>"+text2+"</font>";
textView.setText(Html.fromHtml(text));

2)在一个文本视图中加载文本,但为文本的每个部分设置spannable 3)创建自定义TextView类并计算文本长度然后如果大于您想要在此下面设置另一个textview布局

答案 2 :(得分:0)

我写了一个新的TextViewUtil来处理这种情况并将其发布在这里以防其他人正在寻找这种麻烦:

public class TextViewUtil {

public static void setClickableText(Context context, TextView view, String clickableText, OnClickListener listener) {

    CharSequence text = view.getText();
    String string = text.toString();
    Resources resources = context.getResources();
    ClickSpan span = new ClickSpan(listener, resources.getColor(R.color.background_blue),
            resources.getColor(R.color.universal_profile_blue_link));

    int start = string.indexOf(clickableText);
    int end = start + clickableText.length();
    if (start == -1)
        return;

    if (text instanceof Spannable) {
        ((Spannable) text).setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        view.setText(text);
    } else {
        SpannableString s = SpannableString.valueOf(text);
        s.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        view.setText(s);
    }

    MovementMethod m = view.getMovementMethod();
    if (m == null || !(m instanceof LinkTouchMovementMethod)) {
        view.setMovementMethod(LinkTouchMovementMethod.getInstance());
    }
}
}