添加第二个文本视图将替换线性布局中的第一个文本视图



我正在以编程方式将TextViews添加到LinearLayout中。但是当添加第二个文本视图时,它似乎取代了第一个。

这是代码:

    LinearLayout l = (LinearLayout) findViewById(R.id.contacts_container);
    for (int i = 0; i < array.length(); i++) {
        JSONObject object = array.getJSONObject(i);
        String username = object.getString("username");
        String status = object.getString("status");
// create wrapper
LinearLayout wrapper = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
wrapper.setOrientation(LinearLayout.HORIZONTAL);
int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics());
wrapper.setPadding(padding,padding,padding,padding);
wrapper.setLayoutParams(lp);
l.addView(wrapper);
// add Imageview to wrapper
ImageView image = new ImageView(getApplicationContext());
image.setBackgroundResource(R.drawable.icon_only_dark_crop);
lp = new LinearLayout.LayoutParams((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()), (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()));
lp.setMargins(0, 0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics()), 0);
image.setLayoutParams(lp);
wrapper.addView(image);

        // add linearLayout text wrapper to main wrapper
        LinearLayout textWrapper = new LinearLayout(getApplicationContext());
        textWrapper.setOrientation(LinearLayout.VERTICAL);
        textWrapper.setPadding(0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics()), 0, 0);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 0.9f);
        textWrapper.setLayoutParams(params);
        wrapper.addView(textWrapper);
        // add username TextView to textWrapper
        TextView usernameText = new TextView(getApplicationContext());
        lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        usernameText.setLayoutParams(lp);
        usernameText.setText(username);
        usernameText.setTextColor(Color.parseColor("#FFFFFF"));
        usernameText.setTextSize(0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics()));
        textWrapper.addView(usernameText);
        // add status TextView to textWrapper
        TextView statusText = new TextView(getApplicationContext());
        lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        usernameText.setLayoutParams(lp);
        usernameText.setText(status);
        usernameText.setTextColor(Color.parseColor("#FFFFFF"));
        usernameText.setTextSize(0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics()));
        textWrapper.addView(statusText);

    }

它循环两次,两个"包装器"线性布局被添加到主线性布局中。但是对于每个包装器LinearLayout,它应该添加两个TextViews,但是当我运行应用程序时,只显示状态视图。如果我删除状态视图,用户名视图显示正常。

为什么当状态视图添加到包装器时,用户名视图似乎被隐藏或删除了?

初始化第二个文本视图时复制粘贴的相同布局变量名称

改用这个

    // add username TextView to textWrapper
    TextView usernameText = new TextView(getApplicationContext());
    lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    usernameText.setLayoutParams(lp);
    usernameText.setText(username);
    usernameText.setTextColor(Color.parseColor("#FFFFFF"));
    usernameText.setTextSize(0, (int)    TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics()));
    textWrapper.addView(usernameText);`
    // add status TextView to textWrapper
    TextView statusText = new TextView(getApplicationContext());
    lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    statusText.setLayoutParams(lp);
    statusText.setText(status);
    statusText.setTextColor(Color.parseColor("#FFFFFF"));
    statusText.setTextSize(0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics()));
    textWrapper.addView(statusText);

在第二个文本视图中,检查以下文本。

    TextView **statusText** = new TextView(getApplicationContext());
    **usernameText**.setLayoutParams(lp);
    **usernameText**.setText(status);
    **usernameText**.setTextColor(Color.parseColor("#FFFFFF"));
    **usernameText**.setTextSize(0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics()));
    textWrapper.addView(statusText);

可能是因为您的textWrapper没有指定方向。根据您的需要将其定义为垂直或水平,它应该可以正常工作。

最新更新