11

我在 xml 中有一个空的线性布局。我正在动态添加一些文本视图。

一旦这些文本视图超过 5,那么我将在其中创建另一个线性布局并将文本视图添加到新创建的布局中。最后将这个新布局添加到主布局中。

我可以添加,即在模拟器中它占用该空间,但不会在文本视图中显示信息。

我的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文件如下:

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

        }

    }

}

我哪里错了?

4

3 回答 3

5

您的主要 LinearLayout 设置为 Horizo​​ntal,因此前 5 个文本视图和 layout2 显示在同一行。将 Layout3 添加到 Layout2 会使 Layout3 从主线性布局的最后一个文本视图的右侧显示。在 10 英寸平板电脑上,我只看到 LinearLayout 的前 2 个元素。也许在较小的屏幕上您看不到它们。尝试使用

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

代替

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

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

编辑: 在您的情况下,这应该可以解决问题;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>

和代码:

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

        }

    }
于 2012-07-31T09:45:38.337 回答
0

试试这个,然后你会看到你的代码是好的。

<?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"
android:weightSum="100" >

<HorizontalScrollView
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="80" >

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

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

</LinearLayout>
于 2012-07-31T07:24:50.183 回答
0

实际上你做对了,我只是删除了填充并将所有线性布局的方向设置为垂直。也可以在 xml 文件上修改它,它会起作用 :))) 它只是添加了 10 或 60 的填充,这是父视图的内容。

修改后的 Java 代码:

@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, 10, 10, 10);
                    layout3 = new LinearLayout(this);
                    layout3.setLayoutParams(new LayoutParams(
                            LayoutParams.FILL_PARENT,
                            LayoutParams.WRAP_CONTENT));
                    layout3.setOrientation(LinearLayout.VERTICAL);
               //     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);


            }
        }

    }

}
于 2012-07-31T07:36:52.497 回答