android按钮换⾏_android中组件怎么换⾏
展开全部
应⽤中获取会⽤到需要⾃动换⾏的控件,⽽这并不是⼀般的线性或者相对布局
62616964757a686964616fe78988e69d8331333337613864就能实现的,在此分享下⾃定义控件。原型是在⽹上到的,在此稍作了修改。
这是设计出的样稿,样稿中的较⾼的图⽚是从⼀个数据集中的穿插在另⼀个数据集中的,Textview的长度需要根据⽂字的长度不同⽽设置,⽽左右需要平分,做法如下:
1.将总体分为两个数据集:左&右,并⽤2个LinearLayout分别装⾃定义控件
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_marginTop="5dip">
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
android:layout_width="fill_parent"
android:layout_height="wrap_content">
PredicateLayout>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<PredicateLayout android:id="@+id/righttab"
vertical怎么读
android:layout_width="fill_parent"
android:layout_height="wrap_content">
PredicateLayout>
2.⾃定义控件
public class PredicateLayout extends LinearLayout {
int mLeft, mRight, mTop, mBottom;
Hashtable map = new Hashtable();
public PredicateLayout(Context context) {
super(context);
}
public PredicateLayout(Context context, int horizontalSpacing, int verticalSpacing) {
super(context);
}
public PredicateLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int mWidth = Size(widthMeasureSpec);
int mCount = getChildCount();
int mX = 0;
int mY = 0;
mLeft = 0;
mRight = 0;
mTop = 5;
mBottom = 0;
int j = 0;
View lastview = null;
for (int i = 0; i < mCount; i++) {
final View child = getChildAt(i);
/
/ 此处增加onlayout中的换⾏判断,⽤于计算所需的⾼度
int childw = MeasuredWidth();
int childh = MeasuredHeight();
mX += childw; //将每次⼦控件宽度进⾏统计叠加,如果⼤于设定的⾼度则需要换⾏,⾼度即Top坐标也需重新设置Position position = new Position();
mLeft = getPosition(i - j, i);
mRight = mLeft + MeasuredWidth();
if (mX >= mWidth) {
mX = childw;
mY += childh;
j = i;
mLeft = 0;
mRight = mLeft + MeasuredWidth();
mTop = mY + 5;
//PS:如果发现⾼度还是有问题就得⾃⼰再细调了
}
mBottom = mTop + MeasuredHeight();
mY = mTop; //每次的⾼度必须记录 否则控件会叠加到⼀起
position.left = mLeft;

position.right = mRight;
position.bottom = mBottom;
map.put(child, position);
}
setMeasuredDimension(mWidth, mBottom);
}
@Override
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(1, 1); // default of 1px spacing
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) { // TODO Auto-generated method stub
int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
Position pos = (child);
if (pos != null) {
child.layout(pos.left, p, pos.right, pos.bottom);
} else {
Log.i("MyLayout", "error");
}
}
}
private class Position {
int left, top, right, bottom;
}
public int getPosition(int IndexInRow, int childIndex) {
if (IndexInRow > 0) {
return getPosition(IndexInRow - 1, childIndex - 1)
+ getChildAt(childIndex - 1).getMeasuredWidth() + 8;
}
return getPaddingLeft();
}
}
3.将数据分别填充到左右两个控件中
这应该算是⾃动换⾏经典实例了吧,相信这个搞定以后同类型的需求都不成问题了。已赞过
已踩过<
你对这个回答的评价是?
评论
收起

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。