gpt4 book ai didi

Android - 逐行动态设置textview

转载 作者:行者123 更新时间:2023-11-29 15:43:14 30 4
gpt4 key购买 nike

我必须使用 json 动态创建 textview。现在我成功了Click Here创建但不是以正确的方式。

喜欢这张图片但无法创建。我这样做了,Click Here

这是xml代码--

 <LinearLayout
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:id="@+id/layout_top_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

</LinearLayout>

这是java代码

try {
JSONObject object = new JSONObject(response);
boolean status = object.getBoolean("status");
if (status) {
JSONArray array = object.getJSONArray("topsearches");
for (int i = 0; i < array.length(); i++) {
final TextView textView;

JSONObject jsonObject = array.getJSONObject(i);
String keyword = jsonObject.getString("search-keyord");
String cat_id = jsonObject.getString("category_id");
textView = new TextView(getActivity());
textView.setText(keyword);
textView.setId(i);
textView.setBackgroundResource(R.drawable.border_gray);
textView.setPadding(10, 10, 10, 10);
textView.setTextSize(14);
textView.setLayoutParams(new LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
getLayout_top_search().addView(textView);

textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String query = textView.getText().toString();

}
});

}
}
} catch (Exception e) {
e.printStackTrace();
}

请帮忙。

最佳答案

你想要的只是流式布局

<com.wefika.flowlayout.FlowLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start|top">

<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lorem ipsum" />

</com.wefika.flowlayout.FlowLayout>

您可以使用 Flow Layout 而不是 LinearLayout,它可以帮助您在一行中添加 textview。如果该行没有足够的空间,它将扩展一个新行。然后布局就会变成这样。

更多信息:请阅读thisthis

enter image description here

如果您遇到任何问题,请随时问我。

关于Android - 逐行动态设置textview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37206651/

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