gpt4 book ai didi

android - 创建标签云

转载 作者:太空宇宙 更新时间:2023-11-03 11:36:58 25 4
gpt4 key购买 nike

我需要创建一个基本的标签云。我的目标是添加多个 TextView(Tags),如果数量超过设备宽度,它们会自动适应新行。 (喜欢 Instagram)。

现在,当包含(TextView)标签布局的宽度超过(设备宽度)末尾的 TextView self 包装the TextView at the last gets distorted

我尝试使用 RelativeLayout 但我无法弄清楚其中的逻辑。我查看了this发布但如果有更好或更简单的解决方案。

感谢您的宝贵时间。

结果不一定要像 Instagram。 what i want to achieve

最佳答案

更简洁的解决方案是编写您自己的自定义 ViewGroup 类。找到下面的示例。

如需完整的描述性解释,请访问 How to Create Custom Layout in Android by Extending ViewGroup Class .

public class TagLayout extends ViewGroup {

public TagLayout(Context context) {
super(context);
}

public TagLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}

public TagLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}


@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount();
int curWidth, curHeight, curLeft, curTop, maxHeight;

//get the available size of child view
final int childLeft = this.getPaddingLeft();
final int childTop = this.getPaddingTop();

final int childRight = this.getMeasuredWidth() - this.getPaddingRight();
final int childBottom = this.getMeasuredHeight() - this.getPaddingBottom();

final int childWidth = childRight - childLeft;
final int childHeight = childBottom - childTop;

maxHeight = 0;
curLeft = childLeft;
curTop = childTop;
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
if (child.getVisibility() == GONE)
return;

//Get the maximum size of the child
child.measure(MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.AT_MOST));
curWidth = child.getMeasuredWidth();
curHeight = child.getMeasuredHeight();
//wrap is reach to the end
if (curLeft + curWidth >= childRight) {
curLeft = childLeft;
curTop += maxHeight;
maxHeight = 0;
}
//do the layout
child.layout(curLeft, curTop, curLeft + curWidth, curTop + curHeight);
//store the max height
if (maxHeight < curHeight)
maxHeight = curHeight;
curLeft += curWidth;
}
}
}

要使用 TagLayout,您可以将其添加到您的 Activity/fragment 布局声明中。ma​​in_activity.xml

<com.javatechig.taglayout.TagLayout
android:id="@+id/tagLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

现在我们可以有一个自定义 View ,允许对每个标签项布局进行一定程度的自定义。

tag_layout.xml

<TextView
android:id="@+id/tagTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#a000"
android:padding="10dp"
android:textColor="#fff" />

MainActivity.java最后,您可以从 Activity 类中添加标签项,如下所示。

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TagLayout tagLayout = (TagLayout) findViewById(R.id.tagLayout);
LayoutInflater layoutInflater = getLayoutInflater();
String tag;
for (int i = 0; i <= 20; i++) {
tag = "#tag" + i;
View tagView = layoutInflater.inflate(R.layout.tag_layout, null, false);

TextView tagTextView = (TextView) tagView.findViewById(R.id.tagTextView);
tagTextView.setText(tag);
tagLayout.addView(tagView);
}
}
}

结果

enter image description here

关于android - 创建标签云,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31789771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com