Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 添加第二个TextView将替换线性布局中的第一个TextView_Java_Android_Android Layout_Textview_Android Linearlayout - Fatal编程技术网

Java 添加第二个TextView将替换线性布局中的第一个TextView

Java 添加第二个TextView将替换线性布局中的第一个TextView,java,android,android-layout,textview,android-linearlayout,Java,Android,Android Layout,Textview,Android Linearlayout,我正在以编程方式将textview添加到线性布局中。但当添加第二个TextView时,它似乎取代了第一个 代码如下: LinearLayout l = (LinearLayout) findViewById(R.id.contacts_container); for (int i = 0; i < array.length(); i++) { JSONObject object = array.getJSONObject(i); String

我正在以编程方式将
textview
添加到线性布局中。但当添加第二个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);


    }
LinearLayout l=(LinearLayout)findViewById(R.id.contacts\u容器);
对于(int i=0;i
它循环两次,两个“包装器”线性布局被添加到主线性布局中。但是对于每个包装器LinearLayout,它应该添加两个文本视图,但是当我运行应用程序时,只显示statusView。如果删除statusView,usernameView将显示良好


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

在初始化第二个textview时复制粘贴了相同的布局变量名

用这个代替

    // 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时,复制粘贴了相同的布局变量名称

用这个代替

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

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

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

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

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

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

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

尝试将wrap_内容作为textWrapper布局参数的高度。它是一个水平线性布局,用于包装ImageView和textWrapper。我添加了这两个。我已经尝试了您的代码,您正在为这两个文本视图使用usernameText。对于第二个视图,将其更改为statusText,并将wrap_内容作为textWrapper布局参数的高度。这是一个水平线性布局,用于包装ImageView和textWrapper。我添加了这两个。我已经尝试了您的代码,您正在为这两个文本视图使用usernameText。对于第二个视图,将其更改为statusText,它将是solvedFYI:您不必指定方向,除非您希望它是垂直的。如果未指定任何方向,
LinearLayout
将具有水平方向。仅供参考:除非希望方向垂直,否则不必指定方向。如果没有指定,则
LinearLayout
具有水平方向。唉,我几个小时前就应该停止编码了,唉。谢谢。我已经回答了你的问题,但你还是接受了他的回答。@crm是的,有时候休息一下是很好的P快乐的编码伴侣啊,我应该在几个小时前就停止编码了,唉。谢谢。我已经回答了你的问题,但你还是接受了他的回答。@crm是的,有时候休息一下是很好的快乐的编码伴侣