Android-자식을 래핑하는 LinearLayout Horizontal
Android의 LinearLayout에 대해 설정하여 자식 컨트롤을 적절하게 래핑 할 수있는 속성이 있습니까?
의미-자녀 수가 변경 가능하며 다음과 같이 수평으로 배치하고 싶습니다.
예 : Control1, Control2, Control3, ...
다음을 설정하여 수행합니다.
ll.setOrientation (LinearLayout.HORIZONTAL); foreach (어린이 c) ll.addView (c);
하지만 아이가 많으면 다음 줄로 넘어가는 대신 마지막 아이가 잘립니다.
이것이 어떻게 고칠 수 있는지 아십니까?
2016 년 5 월 현재 Google은 FlexBoxLayout
귀하의 문제를 해결할 자체 를 만들었습니다 .
GitHub 저장소는 https://github.com/google/flexbox-layout 에서 찾을 수 있습니다.
이것은 당신이 원하는 것입니다.
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
/**
*
* @author RAW
*/
public class FlowLayout extends ViewGroup {
private int line_height;
public static class LayoutParams extends ViewGroup.LayoutParams {
public final int horizontal_spacing;
public final int vertical_spacing;
/**
* @param horizontal_spacing Pixels between items, horizontally
* @param vertical_spacing Pixels between items, vertically
*/
public LayoutParams(int horizontal_spacing, int vertical_spacing) {
super(0, 0);
this.horizontal_spacing = horizontal_spacing;
this.vertical_spacing = vertical_spacing;
}
}
public FlowLayout(Context context) {
super(context);
}
public FlowLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
assert (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED);
final int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom();
final int count = getChildCount();
int line_height = 0;
int xpos = getPaddingLeft();
int ypos = getPaddingTop();
int childHeightMeasureSpec;
if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
} else {
childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
}
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
child.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST), childHeightMeasureSpec);
final int childw = child.getMeasuredWidth();
line_height = Math.max(line_height, child.getMeasuredHeight() + lp.vertical_spacing);
if (xpos + childw > width) {
xpos = getPaddingLeft();
ypos += line_height;
}
xpos += childw + lp.horizontal_spacing;
}
}
this.line_height = line_height;
if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.UNSPECIFIED) {
height = ypos + line_height;
} else if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
if (ypos + line_height < height) {
height = ypos + line_height;
}
}
setMeasuredDimension(width, height);
}
@Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(1, 1); // default of 1px spacing
}
@Override
protected android.view.ViewGroup.LayoutParams generateLayoutParams(
android.view.ViewGroup.LayoutParams p) {
return new LayoutParams(1, 1, p);
}
@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
if (p instanceof LayoutParams) {
return true;
}
return false;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount();
final int width = r - l;
int xpos = getPaddingLeft();
int ypos = getPaddingTop();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
final int childw = child.getMeasuredWidth();
final int childh = child.getMeasuredHeight();
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (xpos + childw > width) {
xpos = getPaddingLeft();
ypos += line_height;
}
child.layout(xpos, ypos, xpos + childw, ypos + childh);
xpos += childw + lp.horizontal_spacing;
}
}
}
}
및 XML 파일
/* you must write your package name and class name */
<org.android.FlowLayout
android:id="@+id/flow_layout"
android:layout_marginLeft="5dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
이런 종류의 행동이 필요한 사람을 위해 :
private void populateLinks(LinearLayout ll, ArrayList<Sample> collection, String header) {
Display display = getWindowManager().getDefaultDisplay();
int maxWidth = display.getWidth() - 10;
if (collection.size() > 0) {
LinearLayout llAlso = new LinearLayout(this);
llAlso.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
llAlso.setOrientation(LinearLayout.HORIZONTAL);
TextView txtSample = new TextView(this);
txtSample.setText(header);
llAlso.addView(txtSample);
txtSample.measure(0, 0);
int widthSoFar = txtSample.getMeasuredWidth();
for (Sample samItem : collection) {
TextView txtSamItem = new TextView(this, null,
android.R.attr.textColorLink);
txtSamItem.setText(samItem.Sample);
txtSamItem.setPadding(10, 0, 0, 0);
txtSamItem.setTag(samItem);
txtSamItem.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TextView self = (TextView) v;
Sample ds = (Sample) self.getTag();
Intent myIntent = new Intent();
myIntent.putExtra("link_info", ds.Sample);
setResult("link_clicked", myIntent);
finish();
}
});
txtSamItem.measure(0, 0);
widthSoFar += txtSamItem.getMeasuredWidth();
if (widthSoFar >= maxWidth) {
ll.addView(llAlso);
llAlso = new LinearLayout(this);
llAlso.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
llAlso.setOrientation(LinearLayout.HORIZONTAL);
llAlso.addView(txtSamItem);
widthSoFar = txtSamItem.getMeasuredWidth();
} else {
llAlso.addView(txtSamItem);
}
}
ll.addView(llAlso);
}
}
오래된 질문이지만 누군가가 여기에 올 경우 정확히 그렇게하는 두 개의 라이브러리가 있습니다.
https://github.com/blazsolar/FlowLayout
https://github.com/ApmeM/android-flowlayout
유사하지만 더 간단한 문제에 대한 해결책을 찾고 있습니다. 즉, 하위 텍스트 콘텐츠를 가로 레이아웃으로 래핑하는 것입니다. kape123의 솔루션이 잘 작동합니다. 그러나 ClickableSpan을 사용하여이 문제에 대한 더 간단한 것을 찾으십시오. 간단한 경우에 유용 할 수 있습니다. 단편:
String[] stringSource = new String[sourceList.size()];
for (int i = 0; c < sourceList.size(); i++) {
String text = sourceList.get(i);
stringSource[i] = text;
}
SpannableString totalContent = new SpannableString(TextUtils.join(",", stringSource));
int start = 0;
for (int j = 0; j < stringSource.length(); j++) {
final String text = stringSource[j];
ClickableSpan span = new ClickableSpan() {
@Override
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(true);
ds.setColor(getResources().getColor(R.color.green));
}
@Override
public void onClick(View widget) {
// the text clicked
}
};
int end = (start += text.length());
totalContent.setSpan(span, start, end, 0);
star = end + 1;
}
TextView wrapperView = (TextView) findViewById(horizontal_container_id);
wrapperView.setMovementMethod(LinkMovementMethod.getInstance());
wrapperView.setText(totalContent, BufferType.SPANNABLE);
}
//this method will add image view to liner grid and warp it if no space in new child LinearLayout grid
private void addImageToLinyerLayout(LinearLayout ll , ImageView v)
{
//set the padding and margin and weight
v.setPadding(5, 5, 5, 5);
Display display = getWindowManager().getDefaultDisplay();
int maxWidth = display.getWidth() - 10;
int maxChildeNum = (int) ( maxWidth / (110)) ;
Toast.makeText(getBaseContext(), "c" + v.getWidth() ,
Toast.LENGTH_LONG).show();
//loop through all child of the LinearLayout
for (int i = 0; i < ll.getChildCount(); i++) {
View chidv = ll.getChildAt(i);
Class c = chidv.getClass();
if (c == LinearLayout.class) {
//here we are in the child lay out check to add the imageView if there is space
//Available else we will add it to new linear layout
LinearLayout chidvL = (LinearLayout)chidv;
if(chidvL.getChildCount() < maxChildeNum)
{
chidvL.addView(v);
return;
}
} else{
continue;
}
}
//if you reached here this means there was no roam for adding view so we will
//add new linear layout
LinearLayout childLinyer = new LinearLayout(this);
childLinyer.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
childLinyer.setOrientation(LinearLayout.HORIZONTAL);
ll.addView(childLinyer);
childLinyer.addView(v);
}
위의 방법은 agrid 및 레이아웃과 같이 imgeview를 나란히 추가합니다.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/imageslayout"
></LinearLayout>
이 솔루션을 게시하면 도움이 될 수 있으며 한 번만 절약하고 내 앱에서 사용합니다.
결국 TagView 를 사용했습니다 .
<com.cunoraz.tagview.TagView
android:id="@+id/tag_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp" />
TagView tagGroup = (TagView)findviewById(R.id.tag_view);
//You can add one tag
tagGroup.addTag(Tag tag);
//You can add multiple tag via ArrayList
tagGroup.addTags(ArrayList<Tag> tags);
//Via string array
addTags(String[] tags);
//set click listener
tagGroup.setOnTagClickListener(new OnTagClickListener() {
@Override
public void onTagClick(Tag tag, int position) {
}
});
//set delete listener
tagGroup.setOnTagDeleteListener(new OnTagDeleteListener() {
@Override
public void onTagDeleted(final TagView view, final Tag tag, final int position) {
}
});
Randy Sugianto 'Yuku 's answer 의 코드 수정 버전 과 내가 마침내 함께했던 것 :
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.View.MeasureSpec.*
import android.view.ViewGroup
import androidx.core.content.withStyledAttributes
import androidx.core.view.children
import *.*.*.R
class FlowLayout(context: Context, attributeSet: AttributeSet) : ViewGroup(context, attributeSet) {
private var lineHeight: Int = 0
private var horizontalSpacing = 0F
private var verticalSpacing = 0F
init {
context.withStyledAttributes(attributeSet, R.styleable.FlowLayout) {
horizontalSpacing = getDimension(R.styleable.FlowLayout_horizontalSpacing, 0F)
verticalSpacing = getDimension(R.styleable.FlowLayout_verticalSpacing, 0F)
}
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val width = getSize(widthMeasureSpec) - paddingLeft - paddingRight
var height = getSize(heightMeasureSpec) - paddingTop - paddingBottom
var xPosition = paddingLeft
var yPosition = paddingTop
val childHeightMeasureSpec = makeMeasureSpec(
height, if (getMode(heightMeasureSpec) == AT_MOST) AT_MOST else UNSPECIFIED
)
children.forEach { child ->
if (child.visibility != GONE) {
val layoutParams = child.layoutParams as LayoutParamsWithSpacing
child.measure(makeMeasureSpec(width, AT_MOST), childHeightMeasureSpec)
val childWidth = child.measuredWidth
lineHeight =
Math.max(lineHeight, child.measuredHeight + layoutParams.verticalSpacing)
if (xPosition + childWidth > width) {
xPosition = paddingLeft
yPosition += lineHeight
}
xPosition += childWidth + layoutParams.horizontalSpacing
}
}
if (getMode(heightMeasureSpec) == UNSPECIFIED ||
getMode(heightMeasureSpec) == AT_MOST && yPosition + lineHeight < height
) {
height = yPosition + lineHeight
}
setMeasuredDimension(width, height)
}
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
val width = right - left
var xPosition = paddingLeft
var yPosition = paddingTop
children.forEach { child ->
if (child.visibility != View.GONE) {
val layoutParams = child.layoutParams as LayoutParamsWithSpacing
val childWidth = child.measuredWidth
if (xPosition + childWidth > width) {
xPosition = paddingLeft
yPosition += lineHeight
}
child.layout(
xPosition, yPosition, xPosition + childWidth,
yPosition + child.measuredHeight
)
xPosition += layoutParams.horizontalSpacing
xPosition += childWidth
}
}
}
override fun generateDefaultLayoutParams(): ViewGroup.LayoutParams =
LayoutParamsWithSpacing(1, 1)
override fun generateLayoutParams(layoutParams: LayoutParams) =
LayoutParamsWithSpacing(horizontalSpacing.toInt(), verticalSpacing.toInt())
override fun checkLayoutParams(layoutParams: LayoutParams) =
layoutParams is LayoutParamsWithSpacing
class LayoutParamsWithSpacing(val horizontalSpacing: Int, val verticalSpacing: Int) :
ViewGroup.LayoutParams(0, 0)
}
style / attrs.xml 파일에서 :
<resources>
<declare-styleable name="FlowLayout">
<attr name="horizontalSpacing" format="dimension" />
<attr name="verticalSpacing" format="dimension" />
</declare-styleable>
</resources>
용법:
<*.*.*.*.FlowLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:horizontalSpacing="8dp"
app:verticalSpacing="8dp">
<!-- ... -->
</*.*.*.*.FlowLayout>
Google은 자체 솔루션을 제공합니다. FlowLayout 클래스
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.google.android.material.internal;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.Scope;
import androidx.core.view.MarginLayoutParamsCompat;
import androidx.core.view.ViewCompat;
import com.google.android.material.R.styleable;
@RestrictTo({Scope.LIBRARY_GROUP})
public class FlowLayout extends ViewGroup {
private int lineSpacing;
private int itemSpacing;
private boolean singleLine;
public FlowLayout(Context context) {
this(context, (AttributeSet)null);
}
public FlowLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.singleLine = false;
this.loadFromAttributes(context, attrs);
}
@TargetApi(21)
public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
this.singleLine = false;
this.loadFromAttributes(context, attrs);
}
private void loadFromAttributes(Context context, AttributeSet attrs) {
TypedArray array = context.getTheme().obtainStyledAttributes(attrs, styleable.FlowLayout, 0, 0);
this.lineSpacing = array.getDimensionPixelSize(styleable.FlowLayout_lineSpacing, 0);
this.itemSpacing = array.getDimensionPixelSize(styleable.FlowLayout_itemSpacing, 0);
array.recycle();
}
protected int getLineSpacing() {
return this.lineSpacing;
}
protected void setLineSpacing(int lineSpacing) {
this.lineSpacing = lineSpacing;
}
protected int getItemSpacing() {
return this.itemSpacing;
}
protected void setItemSpacing(int itemSpacing) {
this.itemSpacing = itemSpacing;
}
protected boolean isSingleLine() {
return this.singleLine;
}
public void setSingleLine(boolean singleLine) {
this.singleLine = singleLine;
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int maxWidth = widthMode != -2147483648 && widthMode != 1073741824 ? 2147483647 : width;
int childLeft = this.getPaddingLeft();
int childTop = this.getPaddingTop();
int childBottom = childTop;
int maxChildRight = 0;
int maxRight = maxWidth - this.getPaddingRight();
int finalWidth;
for(finalWidth = 0; finalWidth < this.getChildCount(); ++finalWidth) {
View child = this.getChildAt(finalWidth);
if (child.getVisibility() != 8) {
this.measureChild(child, widthMeasureSpec, heightMeasureSpec);
LayoutParams lp = child.getLayoutParams();
int leftMargin = 0;
int rightMargin = 0;
if (lp instanceof MarginLayoutParams) {
MarginLayoutParams marginLp = (MarginLayoutParams)lp;
leftMargin += marginLp.leftMargin;
rightMargin += marginLp.rightMargin;
}
int childRight = childLeft + leftMargin + child.getMeasuredWidth();
if (childRight > maxRight && !this.isSingleLine()) {
childLeft = this.getPaddingLeft();
childTop = childBottom + this.lineSpacing;
}
childRight = childLeft + leftMargin + child.getMeasuredWidth();
childBottom = childTop + child.getMeasuredHeight();
if (childRight > maxChildRight) {
maxChildRight = childRight;
}
childLeft += leftMargin + rightMargin + child.getMeasuredWidth() + this.itemSpacing;
}
}
finalWidth = getMeasuredDimension(width, widthMode, maxChildRight);
int finalHeight = getMeasuredDimension(height, heightMode, childBottom);
this.setMeasuredDimension(finalWidth, finalHeight);
}
private static int getMeasuredDimension(int size, int mode, int childrenEdge) {
switch(mode) {
case -2147483648:
return Math.min(childrenEdge, size);
case 1073741824:
return size;
default:
return childrenEdge;
}
}
protected void onLayout(boolean sizeChanged, int left, int top, int right, int bottom) {
if (this.getChildCount() != 0) {
boolean isRtl = ViewCompat.getLayoutDirection(this) == 1;
int paddingStart = isRtl ? this.getPaddingRight() : this.getPaddingLeft();
int paddingEnd = isRtl ? this.getPaddingLeft() : this.getPaddingRight();
int childStart = paddingStart;
int childTop = this.getPaddingTop();
int childBottom = childTop;
int maxChildEnd = right - left - paddingEnd;
for(int i = 0; i < this.getChildCount(); ++i) {
View child = this.getChildAt(i);
if (child.getVisibility() != 8) {
LayoutParams lp = child.getLayoutParams();
int startMargin = 0;
int endMargin = 0;
if (lp instanceof MarginLayoutParams) {
MarginLayoutParams marginLp = (MarginLayoutParams)lp;
startMargin = MarginLayoutParamsCompat.getMarginStart(marginLp);
endMargin = MarginLayoutParamsCompat.getMarginEnd(marginLp);
}
int childEnd = childStart + startMargin + child.getMeasuredWidth();
if (!this.singleLine && childEnd > maxChildEnd) {
childStart = paddingStart;
childTop = childBottom + this.lineSpacing;
}
childEnd = childStart + startMargin + child.getMeasuredWidth();
childBottom = childTop + child.getMeasuredHeight();
if (isRtl) {
child.layout(maxChildEnd - childEnd, childTop, maxChildEnd - childStart - startMargin, childBottom);
} else {
child.layout(childStart + startMargin, childTop, childEnd, childBottom);
}
childStart += startMargin + endMargin + child.getMeasuredWidth() + this.itemSpacing;
}
}
}
}
}
이 클래스는 위에서 설명한 FlowLayout 클래스와 유사하게 작동하지만 프로젝트에 새 클래스를 추가해서는 안되며 디자이너는 사용자 정의보다이 클래스에서 더 잘 작동합니다.
참고 URL : https://stackoverflow.com/questions/2961777/android-linearlayout-horizontal-with-wrapping-children
'Programing' 카테고리의 다른 글
Django Rest Framework-ModelSerializer에 사용자 정의 필드를 추가하는 방법 (0) | 2020.10.10 |
---|---|
.NET 용 오이 대안 (0) | 2020.10.10 |
: 첫 자녀가 예상대로 작동하지 않음 (0) | 2020.10.09 |
UTF-8로 인코딩 할 수있는 문자는 몇 개입니까? (0) | 2020.10.09 |
Chrome에서 실행되지 않는 드롭 이벤트 (0) | 2020.10.09 |