线性布局内部线性布局添加但不可见线性、布局、但不

2023-09-07 17:50:38 作者:不要乱闹。

大家好我遇到的XML有一个空的线性布局。我就是动态地添加一些文本,以便它。

Hi everyone i'm having one empty Linear Layout in xml. And i'm adding some text view to it dynamically.

一旦这些textviews超过5然后我创建里面多了一个线性布局,添加文字意见,新创建的布局。最后加入这个新的布局主要布局。

once these textviews exceeds 5 then i'm creating one more linear layout inside it and adding text views to newly created Layout. And finally adding this new layout to main layout.

我能够添加,即在模拟器它占用的空间,但不会显示在textviews的信息。

I'm able to add, i.e in emulator it occupy that space but, will not display the info in the textviews.

我的XML如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/dyn_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dip" >
        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/dyn_button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add TextViews" />

</LinearLayout>

和我的Java文件如下:

and my java file is as follows:

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AddTextViewsDynamically extends Activity implements
        OnClickListener {

    Button button;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_textview_dynamically);

        button = (Button) findViewById(R.id.dyn_button1);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.dyn_button1:
            LinearLayout layout = (LinearLayout) findViewById(R.id.dyn_layout);
            LinearLayout layout2 = null;
            LinearLayout layout3 = null;
            for (int i = 0; i < 10; i++) {
                if (i > 4) {
                    if (i == 5) {
                        layout2 = new LinearLayout(this);
                        layout2.setLayoutParams(new LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.WRAP_CONTENT));
                        layout2.setOrientation(LinearLayout.VERTICAL);
                        layout2.setPadding(10, 60, 10, 10);
                        layout3 = new LinearLayout(this);
                        layout3.setLayoutParams(new LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.WRAP_CONTENT));
                        layout3.setOrientation(LinearLayout.HORIZONTAL);
                        layout3.setPadding(10, 10, 10, 10);
                    }
                    System.out.println("**** Adding text view " + i);
                    TextView text = new TextView(this);
                    text.setText("The Value of i is :" + i);
                    text.setTextSize(12);
                    text.setGravity(Gravity.LEFT);
                    text.setLayoutParams(new LayoutParams(155,
                            LayoutParams.WRAP_CONTENT));
                    layout3.addView(text);

                    if (i == 9) {
                        System.out
                                .println("Added second linear layout to first");
                        layout2.setVisibility(View.VISIBLE);
                        layout2.addView(layout3);
                        layout.addView(layout2);
                    }
                } else {
                    System.out.println("###### Adding text view " + i);
                    TextView text = new TextView(this);
                    text.setText("The Value of i is :" + i);
                    text.setTextSize(12);
                    text.setGravity(Gravity.LEFT);
                    text.setLayoutParams(new LayoutParams(155,
                            LayoutParams.WRAP_CONTENT));
                    layout.addView(text);
                }
            }

        }

    }

}

可以任何一个电话我,我要去哪里错了。并请帮助我。

Can any one tel me where i'm going wrong. And please help me out.

感谢您提前Mouni

Thank You in Advance Mouni

推荐答案

您主要的LinearLayout设置为横向所以第5个文本视图和布局2在同一线所示。添加到Layout3使得布局2的Layout3被从原线性规划的最后文本视图的右侧所示。在一个10英寸的平板电脑我看的只是你的LinearLayout的第2个元素。也许一个小屏幕上,你看不出来。尝试使用

Your primary LinearLayout is set to Horizontal so the first 5 text view and the layout2 are shown on the same line. Adding Layout3 to Layout2 makes the Layout3 to be shown from the right of the last text view from primary Linear Layout. On a 10 inch tablet i see only the first 2 elements of your LinearLayout. Perhaps on a smaller screen you don't see them. Try using

text.setLayoutParams(new LayoutParams(50, LayoutParams.WRAP_CONTENT));

而不是

text.setLayoutParams(new LayoutParams(155, LaoutParams.WRAP_CONTENT));

,你应该看到您所有的文本视图。

and you should see all your text views.

编辑:你的情况,这应该做的伎俩;XML:

EDIT : In your case this should do the trick; xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/dyn_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dip" >
        </LinearLayout>

        <LinearLayout
            android:id="@+id/dyn_layout2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:visibility="gone"
            android:padding="10dip" >
        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/dyn_button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add TextViews" />

</LinearLayout>

和code:

@Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.dyn_button1:
            LinearLayout layout = (LinearLayout) findViewById(R.id.dyn_layout);
            LinearLayout layout2 = (LinearLayout) findViewById(R.id.dyn_layout2);
            LinearLayout layout3 = null;
            for (int i = 0; i < 10; i++) {
                if (i > 4) {
                    if (i == 5) {
                        layout2.setPadding(10, 60, 10, 10);
                        layout3 = new LinearLayout(this);
                        layout3.setLayoutParams(new LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.WRAP_CONTENT));
                        layout3.setOrientation(LinearLayout.HORIZONTAL);
                        layout3.setPadding(10, 10, 10, 10);
                    }
                    System.out.println("**** Adding text view " + i);
                    TextView text = new TextView(this);
                    text.setText("The Value of i is :" + i);
                    text.setTextSize(12);
                    text.setGravity(Gravity.LEFT);
                    text.setLayoutParams(new LayoutParams(155,
                            LayoutParams.WRAP_CONTENT));
                    layout3.addView(text);

                    if (i == 9) {
                        System.out
                                .println("Added second linear layout to first");
                        layout2.setVisibility(View.VISIBLE);
                        layout2.addView(layout3);
                    }
                } else {
                    System.out.println("###### Adding text view " + i);
                    TextView text = new TextView(this);
                    text.setText("The Value of i is :" + i);
                    text.setTextSize(12);
                    text.setGravity(Gravity.LEFT);
                    text.setLayoutParams(new LayoutParams(155,
                            LayoutParams.WRAP_CONTENT));
                    layout.addView(text);
                }
            }

        }

    }