Android 两个view并列显示

两个view并列显示,第二个view跟在第一个view的后面,如果长度过长,则第一个view宽度缩小。

直接上代码  用就完了!

public class TextWithImageLayout extends LinearLayout {

    private final int MARGIN = 40;
    private int mMargin;

    private int[] mChildWidthList;

    public TextWithImageLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mMargin = MARGIN;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int count = getChildCount();
        View firstChild = getChildAt(0);

        if (count > 1) {
            int firstChildWidth = firstChild.getMeasuredWidth();
            int firstChildHeight = firstChild.getMeasuredHeight();
            int otherChildrenWidth = 0;
            int otherChildrenHeight = 0;

            for (int i = 1; i < count; i++) {
                View child = getChildAt(i);
                int width;
                if (mChildWidthList == null) {
                    width = child.getMeasuredWidth();
                } else {
                    width = mChildWidthList[i - 1];
                }
                otherChildrenWidth += width;
                child.measure(width | MeasureSpec.EXACTLY, child.getMeasuredHeight() | MeasureSpec.EXACTLY);
                otherChildrenHeight = child.getMeasuredHeight();
            }

            int margin = mMargin * (count - 1);
            if (firstChildWidth + otherChildrenWidth + margin > widthSize) {
                firstChildWidth = widthSize - otherChildrenWidth - margin;
            }

            heightSize = firstChildHeight > otherChildrenHeight ? firstChildHeight : otherChildrenHeight;

            firstChild.measure(firstChildWidth | MeasureSpec.EXACTLY,
                    firstChildHeight | MeasureSpec.EXACTLY);
        } else {
            firstChild.measure(firstChild.getMeasuredWidth(), firstChild.getMeasuredHeight());
            heightSize = firstChild.getMeasuredHeight();
        }

        setMeasuredDimension(widthSize, heightSize);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        int count = getChildCount();

        int left = 0;
        for (int i = 0; i < count; i++) {

            final View child = this.getChildAt(i);
            int width = child.getMeasuredWidth();
            int height = child.getMeasuredHeight();
            int top = (b - t) / 2 - height / 2;

            child.layout(left, top, left + width, top + height);

            left = left + width + mMargin;
        }

    }

    public int[] getChildWidthList() {
        return mChildWidthList;
    }

    public void setChildWidthList(int[] childWidthList) {
        this.mChildWidthList = childWidthList;
    }
}

 

发布了19 篇原创文章 · 获赞 28 · 访问量 4896
展开阅读全文

在vertical布局中如何把两个TextView放在同一行

12-14

我想在ImageView上面添加4个textview:"Title" "times" "age" and "informations". 它们都是全局的横向布局。但现在4个textview是在一个垂直的布局。现在想让"times" 和"age"在一个水平线上,但是不能使用垂直布局。 以下是XML代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/imgLink" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Titre" android:textSize="8sp" android:textStyle="bold" /> <TextView android:id="@+id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="8sp" android:text="age" /> <TextView android:id="@+id/age" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="8sp" android:text="age" /> <TextView android:id="@+id/information" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" android:textSize="8sp" android:text="phrase" /> </LinearLayout> 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术工厂 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览