【问题标题】:linear layout inside linear layout is adding but is not visible线性布局内的线性布局正在添加但不可见
【发布时间】:2012-07-31 07:09:45
【问题描述】:

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

        }

    }

}

我哪里错了?

【问题讨论】:

  • 你能说出你为什么这样做,以便可以提供替代和最佳解决方案吗?
  • 我将从服务中动态获取一些信息,我必须在每行的文本视图 5 中显示这些信息。如果它更多,则在下一行。
  • 我还有其他方法可以做到吗..??请告诉我
  • 我实现了您的代码,并且“i 的值是:0...”显示正常。请提供更多信息或发布清单和其他元素。
  • @cosmincalistru:是的,你是对的,你会得到一行 5 个文本字段。但根据我的代码,我应该得到两行 10 个文本字段数据..

标签: android android-linearlayout


【解决方案1】:

您的主要 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);
                }
            }

        }

    }

【讨论】:

  • 非常感谢。是的,所有 10 个文本视图都是垂直添加的。但是我只想要一行中的 5 个,下一个中的另一个 5 我应该怎么做才能以这种方式获得布局..?请帮帮我..
  • 是的,它成功了,但几乎没有什么变化,因为我不会在 xml 中添加布局..它将被动态添加..现在它工作正常..:) 再次感谢你..
【解决方案2】:

其实你做对了,我只是删除了填充并将所有线性布局的方向设置为垂直。也可以在 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);


            }
        }

    }

}

【讨论】:

  • 你好 abhy,你是得到两行信息还是只有一行..?
  • 是的,它适用于垂直布局。但我希望它在水平布局中,即每行 5 个值。我应该为此做些什么..??
【解决方案3】:

试试这个,你会发现你的代码没问题。

<?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>

【讨论】:

  • 没有成功.. :( 差异只是按钮出现在屏幕的末尾.. 有什么我需要改变的..??
  • 查看我编辑的答案,如果要水平添加文本视图,则需要使用 Horizo​​ntalScrollView 而不是常规视图。让我知道它是否有效,(顺便说一句,我试过了,它对我有用)
  • 我就是不明白,你到底想要什么?一切正常,“信息”是什么意思?如果你说清楚了,我可以给你指路。
  • 信息不过是数据。我希望数据作为水平线性布局的 5 个文本视图,如果我得到的数据超过 5 个,那么我必须再创建一个这样的布局并添加它。那时我应该有两行数据,每行有五个文本字段水平排列..我想现在你得到了我想要的..如果没有请告诉我..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-26
  • 1970-01-01
相关资源
最近更新 更多