對齊使用循環創建動態按鈕和調整按鈕如.. 創建動態按鈕,並在佈局中的Android
回答
You can use Flow FlowLayout
Here is code for FlowLayout, include this in your project
public class FlowLayout extends ViewGroup {
private int mGravity = (isIcs() ? Gravity.START : Gravity.LEFT) | Gravity.TOP;
private final List<List<View>> mLines = new ArrayList<List<View>>();
private final List<Integer> mLineHeights = new ArrayList<Integer>();
private final List<Integer> mLineMargins = new ArrayList<Integer>();
public FlowLayout(Context context) {
this(context, null);
}
public FlowLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public FlowLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.FlowLayout, defStyle, 0);
try {
int index = a.getInt(R.styleable.FlowLayout_android_gravity, -1);
if(index > 0) {
setGravity(index);
}
} finally {
a.recycle();
}
}
/**
* {@inheritDoc}
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
int width = 0;
int height = getPaddingTop() + getPaddingBottom();
int lineWidth = 0;
int lineHeight = 0;
int childCount = getChildCount();
for(int i = 0; i < childCount; i++) {
View child = getChildAt(i);
boolean lastChild = i == childCount - 1;
if(child.getVisibility() == View.GONE) {
if(lastChild) {
width = Math.max(width, lineWidth);
height += lineHeight;
}
continue;
}
measureChildWithMargins(child, widthMeasureSpec, lineWidth, heightMeasureSpec, height);
LayoutParams lp = (LayoutParams) child.getLayoutParams();
int childWidthMode = MeasureSpec.AT_MOST;
int childWidthSize = sizeWidth;
int childHeightMode = MeasureSpec.AT_MOST;
int childHeightSize = sizeHeight;
if(lp.width == LayoutParams.MATCH_PARENT) {
childWidthMode = MeasureSpec.EXACTLY ;
childWidthSize -= lp.leftMargin + lp.rightMargin;
} else if(lp.width >= 0) {
childWidthMode = MeasureSpec.EXACTLY;
childWidthSize = lp.width;
}
if(lp.height >= 0) {
childHeightMode = MeasureSpec.EXACTLY;
childHeightSize = lp.height;
} else if (modeHeight == MeasureSpec.UNSPECIFIED) {
childHeightMode = MeasureSpec.UNSPECIFIED;
childHeightSize = 0;
}
child.measure(
MeasureSpec.makeMeasureSpec(childWidthSize, childWidthMode),
MeasureSpec.makeMeasureSpec(childHeightSize, childHeightMode)
);
int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
if(lineWidth + childWidth > sizeWidth) {
width = Math.max(width, lineWidth);
lineWidth = childWidth;
height += lineHeight;
lineHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
} else {
lineWidth += childWidth;
lineHeight = Math.max(lineHeight, child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
}
if(lastChild) {
width = Math.max(width, lineWidth);
height += lineHeight;
}
}
width += getPaddingLeft() + getPaddingRight();
setMeasuredDimension(
(modeWidth == MeasureSpec.EXACTLY) ? sizeWidth : width,
(modeHeight == MeasureSpec.EXACTLY) ? sizeHeight : height);
}
/**
* {@inheritDoc}
*/
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
mLines.clear();
mLineHeights.clear();
mLineMargins.clear();
int width = getWidth();
int height = getHeight();
int linesSum = getPaddingTop();
int lineWidth = 0;
int lineHeight = 0;
List<View> lineViews = new ArrayList<View>();
float horizontalGravityFactor;
switch ((mGravity & Gravity.HORIZONTAL_GRAVITY_MASK)) {
case Gravity.LEFT:
default:
horizontalGravityFactor = 0;
break;
case Gravity.CENTER_HORIZONTAL:
horizontalGravityFactor = .5f;
break;
case Gravity.RIGHT:
horizontalGravityFactor = 1;
break;
}
for(int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
if(child.getVisibility() == View.GONE) {
continue;
}
LayoutParams lp = (LayoutParams) child.getLayoutParams();
int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
int childHeight = child.getMeasuredHeight() + lp.bottomMargin + lp.topMargin;
if(lineWidth + childWidth > width) {
mLineHeights.add(lineHeight);
mLines.add(lineViews);
mLineMargins.add((int) ((width - lineWidth) * horizontalGravityFactor) + getPaddingLeft());
linesSum += lineHeight;
lineHeight = 0;
lineWidth = 0;
lineViews = new ArrayList<View>();
}
lineWidth += childWidth;
lineHeight = Math.max(lineHeight, childHeight);
lineViews.add(child);
}
mLineHeights.add(lineHeight);
mLines.add(lineViews);
mLineMargins.add((int) ((width - lineWidth) * horizontalGravityFactor) + getPaddingLeft());
linesSum += lineHeight;
int verticalGravityMargin = 0;
switch ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) ) {
case Gravity.TOP:
default:
break;
case Gravity.CENTER_VERTICAL:
verticalGravityMargin = (height - linesSum)/2;
break;
case Gravity.BOTTOM:
verticalGravityMargin = height - linesSum;
break;
}
int numLines = mLines.size();
int left;
int top = getPaddingTop();
for(int i = 0; i < numLines; i++) {
lineHeight = mLineHeights.get(i);
lineViews = mLines.get(i);
left = mLineMargins.get(i);
int children = lineViews.size();
for(int j = 0; j < children; j++) {
View child = lineViews.get(j);
if(child.getVisibility() == View.GONE) {
continue;
}
LayoutParams lp = (LayoutParams) child.getLayoutParams();
// if height is match_parent we need to remeasure child to line height
if(lp.height == LayoutParams.MATCH_PARENT) {
int childWidthMode = MeasureSpec.AT_MOST;
int childWidthSize = lineWidth;
if(lp.width == LayoutParams.MATCH_PARENT) {
childWidthMode = MeasureSpec.EXACTLY;
} else if(lp.width >= 0) {
childWidthMode = MeasureSpec.EXACTLY;
childWidthSize = lp.width;
}
child.measure(
MeasureSpec.makeMeasureSpec(childWidthSize, childWidthMode),
MeasureSpec.makeMeasureSpec(lineHeight - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY)
);
}
int childWidth = child.getMeasuredWidth();
int childHeight = child.getMeasuredHeight();
int gravityMargin = 0;
if(Gravity.isVertical(lp.gravity)) {
switch (lp.gravity) {
case Gravity.TOP:
default:
break;
case Gravity.CENTER_VERTICAL:
case Gravity.CENTER:
gravityMargin = (lineHeight - childHeight - lp.topMargin - lp.bottomMargin)/2 ;
break;
case Gravity.BOTTOM:
gravityMargin = lineHeight - childHeight - lp.topMargin - lp.bottomMargin;
break;
}
}
child.layout(left + lp.leftMargin,
top + lp.topMargin + gravityMargin + verticalGravityMargin,
left + childWidth + lp.leftMargin,
top + childHeight + lp.topMargin + gravityMargin + verticalGravityMargin);
left += childWidth + lp.leftMargin + lp.rightMargin;
}
top += lineHeight;
}
}
@Override
protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
return new LayoutParams(p);
}
/**
* {@inheritDoc}
*/
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
}
/**
* {@inheritDoc}
*/
@Override
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
}
@Override protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return super.checkLayoutParams(p) && p instanceof LayoutParams;
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void setGravity(int gravity) {
if(mGravity != gravity) {
if((gravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) == 0) {
gravity |= isIcs() ? Gravity.START : Gravity.LEFT;
}
if((gravity & Gravity.VERTICAL_GRAVITY_MASK) == 0) {
gravity |= Gravity.TOP;
}
mGravity = gravity;
requestLayout();
}
}
public int getGravity() {
return mGravity;
}
/**
* @return <code>true</code> if device is running ICS or grater version of Android.
*/
private static boolean isIcs() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
}
public static class LayoutParams extends MarginLayoutParams {
public int gravity = -1;
public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.FlowLayout_Layout);
try {
gravity = a.getInt(R.styleable.FlowLayout_Layout_android_layout_gravity, -1);
} finally {
a.recycle();
}
}
public LayoutParams(int width, int height) {
super(width, height);
}
public LayoutParams(ViewGroup.LayoutParams source) {
super(source);
}
}
}
in XML
<com.example.util.FlowLayout
android:id="@+id/flow"
android:layout_width="match_parent"
android:layout_height="match_parent">
在把這個值繪製
&在
創建attrs.xml
然後,您可以在此添加按鈕根據需要動態佈局。
https://github.com/mcxiaoke/Android-Next/blob/master/samples/src/main/java/com/mcxiaoke/next/samples/layout/FlowLayoutA.java –
@ user3040153在R.styleable中顯示錯誤。 FlowLayout和R.styleable.FlowLayout_android_gravity將爲它提供樣式。 – prabhakaran
把這個attrs.xml <聲明-設置樣式名稱= 「的FlowLayout」>
使用流式佈局:
查找完整implemation在下面的鏈接
https://github.com/blazsolar/FlowLayout
http://hzqtc.github.io/2013/12/android-custom-layout-flowlayout.html
- 1. 動態按鈕Android佈局
- 2. 動態按鈕佈局android
- 3. 使用android中的兩個佈局創建動態按鈕
- 4. Android:創建動態佈局
- 5. 在ListView中動態創建按鈕[android]
- 6. Android:動態添加按鈕佈局
- 7. Android按鈕浮動佈局
- 8. Android:如何動態創建此佈局?
- 9. 創建CardView佈局和浮動操作按鈕的Android
- 10. 如何在android中創建線性佈局和中心按鈕?
- 11. 的Android訪問按鈕動態創建
- 12. 如何在佈局中的行上放置動態創建的按鈕?
- 13. 動態創建佈局
- 14. 動態創建佈局
- 15. 問題在佈局(Android)中創建了一個按鈕?
- 16. 在Android中動態添加按鈕佈局
- 17. Xcode中的UIWebView創建佈局動態
- 18. 如何在Android中保存(用戶創建的)動態佈局?
- 19. 使用服務器響應中的按鈕創建Android佈局
- 20. Android動態創建線性佈局並排列左邊
- 21. 動態創建並添加框架佈局android
- 22. 動態地調整桌面佈局中的按鈕大小(Android)
- 23. ANdroid:動態添加圖像按鈕到表佈局中的行
- 24. 按鈕點擊動態添加布局並從佈局中獲取文本?
- 25. Android中的按下按鈕佈局
- 26. 在matlab中動態創建按鈕
- 27. 在Flex中動態創建按鈕
- 28. 在Java中動態創建按鈕
- 29. 在thymeleaf表中動態創建按鈕
- 30. 在PyQt中創建動態按鈕
肯定。它可能的 –
http://stackoverflow.com/questions/1851633/how-to-add-a-button-dynamically-in-android –
use'GridLayout' –