添加第二个TextView替换LinearLayout

时间:2016-02-10 20:25:28

标签: java android android-layout textview android-linearlayout

我以编程方式将TextViews添加到LinearLayout。但是当添加第二个TextView时,它似乎取代了第一个。

以下是代码:

    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);


    }

它循环两次,两个“包装器”LinearLayouts被添加到主LinearLayout中。但是对于每个包装器LinearLayout,它应该添加两个TextView,但是当我运行应用程序时,只显示statusView。如果我删除statusView,则usernameView显示正常。

为什么当statusView被添加到包装器时,usernameView似乎被隐藏或删除了?

3 个答案:

答案 0 :(得分:1)

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

使用此代替

    // 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);

答案 1 :(得分:1)

在第二个textview中,查看以下文字。

    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);

答案 2 :(得分:0)

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