मेरे ऐप में मैंने कोड से क्षैतिज रैखिक लेआउट में कई दृश्य जोड़े। मेरी समस्या तब होती है जब विचारों की संख्या में उनकी पंक्ति में एकल दिखाई नहीं देता है और स्क्रीन आगे जाती है। क्या कोई लेआउट है जिसमें मैं गतिशील रूप से दृश्य जोड़ सकता हूं और यदि निम्न दृश्य पहली पंक्ति में फिट नहीं होता है, तो यह दूसरी पंक्ति में रखा जाता है। सबसे पहले मुझे विचारों की संख्या पता नहीं है हालांकि मैं सबकुछ गतिशील चाहता हूं। कुछ इस तरह: इस लेआउट एकाधिक पंक्तियों के साथ एंड्रॉइड लीनियरआउट
6
A
उत्तर
4
यहाँ मुझे लगता है कि धन्यवाद @psink करने के लिए एक महान पुस्तकालय पाया। https://github.com/ApmeM/android-flowlayout पूरी तरह से काम करता है।
2
उपयोग
package com.shashi.app.utils;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import com.shashi.app.R;
public class FlowLayout extends ViewGroup {
private int mHorizontalSpacing;
private int mVerticalSpacing;
private Paint mPaint;
public FlowLayout(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.FlowLayout);
try {
mHorizontalSpacing = a.getDimensionPixelSize(
R.styleable.FlowLayout_horizontalSpacing, 0);
mVerticalSpacing = a.getDimensionPixelSize(
R.styleable.FlowLayout_verticalSpacing, 0);
} finally {
a.recycle();
}
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(0xffff0000);
mPaint.setStrokeWidth(2.0f);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec)
- getPaddingRight();
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
boolean growHeight = widthMode != MeasureSpec.UNSPECIFIED;
int width = 0;
int height = getPaddingTop();
int currentWidth = getPaddingLeft();
int currentHeight = 0;
boolean breakLine = false;
boolean newLine = false;
int spacing = 0;
final int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
measureChild(child, widthMeasureSpec, heightMeasureSpec);
LayoutParams lp = (LayoutParams) child.getLayoutParams();
spacing = mHorizontalSpacing;
if (lp.horizontalSpacing >= 0) {
spacing = lp.horizontalSpacing;
}
if (growHeight
&& (breakLine || currentWidth + child.getMeasuredWidth() > widthSize)) {
height += currentHeight + mVerticalSpacing;
currentHeight = 0;
width = Math.max(width, currentWidth - spacing);
currentWidth = getPaddingLeft();
newLine = true;
} else {
newLine = false;
}
lp.x = currentWidth;
lp.y = height;
currentWidth += child.getMeasuredWidth() + spacing;
currentHeight = Math.max(currentHeight, child.getMeasuredHeight());
breakLine = lp.breakLine;
}
if (!newLine) {
height += currentHeight;
width = Math.max(width, currentWidth - spacing);
}
width += getPaddingRight();
height += getPaddingBottom();
setMeasuredDimension(resolveSize(width, widthMeasureSpec),
resolveSize(height, heightMeasureSpec));
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
LayoutParams lp = (LayoutParams) child.getLayoutParams();
child.layout(lp.x, lp.y, lp.x + child.getMeasuredWidth(), lp.y
+ child.getMeasuredHeight());
}
}
@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
boolean more = super.drawChild(canvas, child, drawingTime);
LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (lp.horizontalSpacing > 0) {
float x = child.getRight();
float y = child.getTop() + child.getHeight()/2.0f;
canvas.drawLine(x, y - 4.0f, x, y + 4.0f, mPaint);
canvas.drawLine(x, y, x + lp.horizontalSpacing, y, mPaint);
canvas.drawLine(x + lp.horizontalSpacing, y - 4.0f, x
+ lp.horizontalSpacing, y + 4.0f, mPaint);
}
if (lp.breakLine) {
float x = child.getRight();
float y = child.getTop() + child.getHeight()/2.0f;
canvas.drawLine(x, y, x, y + 6.0f, mPaint);
canvas.drawLine(x, y + 6.0f, x + 6.0f, y + 6.0f, mPaint);
}
return more;
}
@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return p instanceof LayoutParams;
}
@Override
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
}
@Override
protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
return new LayoutParams(p.width, p.height);
}
public static class LayoutParams extends ViewGroup.LayoutParams {
int x;
int y;
public int horizontalSpacing;
public boolean breakLine;
public LayoutParams(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.FlowLayout_LayoutParams);
try {
horizontalSpacing = a
.getDimensionPixelSize(
R.styleable.FlowLayout_LayoutParams_layout_horizontalSpacing,
-1);
breakLine = a.getBoolean(
R.styleable.FlowLayout_LayoutParams_layout_breakLine,
false);
} finally {
a.recycle();
}
}
public LayoutParams(int w, int h) {
super(w, h);
}
}
}
एक्सएमएल उपयोग
<com.shashi.app.utils.FlowLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:paddingRight="15dp"
app:horizontalSpacing="10dp"
app:verticalSpacing="10dp" >
your views here
</com.shashi.app.utils.FlowLayout>
संबंधित मुद्दे
- 1. 1 क्वेरी के साथ एकाधिक पंक्तियों डालने
- 2. एकाधिक पंक्तियों के साथ ExtJS टूलबार
- 3. मैं एंड्रॉइड लीनियरआउट के आसपास सीमा कैसे बना सकता हूं?
- 4. एकाधिक पंक्तियों
- 5. एकाधिक पंक्तियों
- 6. स्प्लिट एक एकाधिक पंक्तियों
- 7. योग एकाधिक पंक्तियों
- 8. एकाधिक पंक्तियों पर सीमा
- 9. क्या एकाधिक पंक्तियों के साथ टेबललेस चयन करना संभव है?
- 10. ओरेकल अलग शीर्षक के साथ एक में एकाधिक पंक्तियों गठबंधन
- 11. टीएसक्यूएल नामित पंक्तियों के साथ एकाधिक कॉलम unpivot संभव है?
- 12. एकाधिक मानों और एकाधिक स्थितियों के साथ एकाधिक पंक्तियों को अपडेट करें mysql
- 13. एकाधिक गतिविधियों के साथ एंड्रॉइड ऐप
- 14. एकाधिक पैरामीटर्स के साथ एंड्रॉइड बहुवचन स्ट्रिंग्स
- 15. एकाधिक पंक्तियों को कैसे हटाएं?
- 16. केंद्र एक लीनियरआउट और "प्रतिशत" मार्जिन
- 17. एकाधिक कॉलम के साथ एंड्रॉइड में एक टेबल कैसे बनाएं?
- 18. "unstack" एक पांडा एकाधिक पंक्तियों
- 19. एकाधिक पंक्तियों पर 'sed' कमांड
- 20. एकाधिक शर्तों के साथ एसक्यूएल एंड्रॉइड क्वेरी फॉर्मूलेशन
- 21. सूची दृश्य पंक्तियों एंड्रॉइड
- 22. एक बैंड में एकाधिक पंक्तियों के साथ डेटा ओवरफ़्लो के साथ एक पंक्ति को खींचें
- 23. एंड्रॉइड वरीयता स्क्रीन शीर्षक दो पंक्तियों में
- 24. एकाधिक पंक्तियों की तुलना कैसे करें?
- 25. आईडी द्वारा एकाधिक पंक्तियों का चयन करना,
- 26. एंड्रॉइड - एकाधिक, एक साथ स्ट्रीमिंग ऑडियो
- 27. हाइबरनेट मानदंड बिल्डर एकाधिक पंक्तियों को एक
- 28. एंड्रॉइड एडाप्टर एकाधिक getView
- 29. डेटाटेबल में एकाधिक पंक्तियों को कैसे हटाएं?
- 30. एसक्यूएल - एकाधिक पंक्तियों का चयन कैसे करें
मुझे ऐसा कुछ नहीं लगता है, जो मुझे पता है। क्या आप सिर्फ बच्चों की चौड़ाई नहीं जोड़ सकते हैं और यदि यह माता-पिता की चौड़ाई से अधिक है, तो एक नया क्षैतिज रैखिक स्तर बनाएं और उन्हें इसके बजाय जोड़ें? –
@ बिलीनोमेट्स मैं जानना चाहता हूं कि इस उद्देश्य के लिए लिखी गई लाइब्रेरी है या ऐसा करने का एक आसान तरीका है। आपको पता है कि माता-पिता को जोड़ने से पहले दृश्यों की चौड़ाई मुश्किल हो रही है। –
@ मोहम्मद गफोरियन: कृपया यहां देखें कि यह मदद करता है: https://stackoverflow.com/questions/549451/line-breaking-widget-layout-for-android/560958#560958 –