Android - 以行方式动态设置textview

时间:2016-05-13 09:45:17

标签: android android-linearlayout textview

我必须动态创建带有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();
            }

请帮忙。

4 个答案:

答案 0 :(得分:3)

您想要的只是SELECT

Flow Layout

而不是<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> 您可以使用LinearLayout这可以帮助您连续添加textview。如果行没有足够的空间,它将扩展一个新行。然后布局就会变成这样。

更多信息:请阅读thisthis

enter image description here

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

答案 1 :(得分:1)

TextView tvContent = new TextView(getActivity());
    medicineName.add(tvContent);
    LinearLayout.LayoutParams tvParam = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    tvContent.setLayoutParams(tvParam);
    tvContent.setGravity(Gravity.LEFT);
    tvContent.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
    tvContent.setText(name);
    tvContent.setTextSize(16);
    layout.addView(tvContent);

在onPostExecute()方法中使用它

答案 2 :(得分:0)

        LinearLayout layout = new LinearLayout(getActivity());
    LinearLayout.LayoutParams lLayoutlayoutParams = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    lLayoutlayoutParams.setMargins(0, 8, 8, 8);
    layout.setLayoutParams(lLayoutlayoutParams);
    layout.setPadding((int) (fDimRatio * 8), (int) (fDimRatio * 8),
            (int) (fDimRatio * 8), (int) (fDimRatio * 8));
    layout.setOrientation(LinearLayout.VERTICAL);



TextView tvContent = new TextView(getActivity());
    medicineName.add(tvContent);
    LinearLayout.LayoutParams tvParam = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    tvContent.setLayoutParams(tvParam);
    tvContent.setGravity(Gravity.LEFT);
    tvContent.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
    tvContent.setText(name);
    tvContent.setTextSize(16);
    layout.addView(tvContent);


linearLayout.addView(layout);

linearLayout是xml

中linearlayout的id
linearLayout = (LinearLayout) view.findViewById(R.id.container);

fDimRatio值是

private float fDimRatio = 1.0f; // /xml

答案 3 :(得分:0)

如果您想要图片1,请更改android:orientation =“horizo​​ntal”