无法在Android中显示ScrollView和textView(以编程方式添加)

时间:2015-02-18 13:25:23

标签: java android xml android-layout

我正在开发一个Android项目,我必须从JSONArray写入数据。我试图为每个数组输入使用TextView。所以我以前编码在我现有的布局上添加一个scrollView并添加一个TextView init。代码工作完美,没有任何错误,但是当我编译它时,UI中没有任何内容。 (在我的手机中)后来,我尝试将另一个TextView添加到现有的LinearLayout而不是ScrollView,它也没有工作。我检查了其他问题和论坛,在我看来,我已经完成/写了他们所做的一切,我仍然找不到我的错误。

UPDATE 1 :我已将ScrollView添加到另一个LinearLayout中,并在其中保留另一个LinearLayout。 TextView正在ScrollView内的LinearLayout中添加。外部LinearLayout持有另一个TextView,只发布异常日志(我的avd内核崩溃)。

UPDATE 2 我已经使scrollView的高度为WRAP_CONTENT,并在其下方添加了另外四个TextView。现在,应用程序加载显示Top TextView和最后一个TextView组之间存在相当大的差距。但仍然,ScrollView没有显示任何内容。

我的代码已更新&如下:

Java代码:

    TextView t1 = (TextView)findViewById(R.id.t1);
    LinearLayout mainLinear = (LinearLayout)findViewById(R.id.mainLinear);

    try {
        LinearLayout linear =(LinearLayout)findViewById(R.id.scroll_container);
        linear.setBackgroundColor(0xABCDEF);


        //for (int i = 0; i < 10; i++) {
            TextView tv = new TextView(getApplicationContext());
            tv.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            ));
            tv.setTextSize(25);
            //tv.setId(1);
            tv.setTextColor(0x000000);
            tv.setPadding(10, 10, 10, 10);
            tv.setText("I am serial " + 0 + "\n" + "is there something      more");
            linear.addView(tv);

        TextView tv1 = new TextView(getApplicationContext());
        tv1.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        ));
        tv1.setTextSize(25);
        //tv.setId(1);
        tv1.setTextColor(0x000000);
        tv1.setPadding(10, 10, 10, 10);
        tv1.setText("I am serial 3 " + 1 + "\n" + "is there something more");
        linear.addView(tv1);



        //tv.append("my name is apple " + 0 + "\n");

            TextView tv5 = new TextView(getApplicationContext());
            tv5.setLayoutParams(new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT
               ));
                tv5.setTextSize(25);
               //tv.setId(1);
                tv5.setTextColor(0x000000);
                tv5.setPadding(10, 10, 10, 10);
                tv5.setText("Linear Layout e" + 0 + "\n");
                mainLinear.addView(tv5);




        // }
    }
    catch (Exception e)
    {
        e.printStackTrace();
        t1.setText("<><>"+e);
    }

我的布局XML文件:

<LinearLayout 
android:id="@+id/mainLinear">

<TextView android:text="Word: Monkey Kaka"
    android:textSize="25dp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/t1"
    android:textColor="#000000"/>
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/ScrollView">
    <LinearLayout
        android:id="@+id/scroll_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />
</ScrollView>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="25dp"
    android:text="Test 1" />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Test 2"
    android:textSize="25dp"
    />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Test 3"
    android:textSize="25dp"
    />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Test 4"
    android:textSize="25dp"
    />
</LinearLayout>

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

ScrollView窗口小部件只能托管一个直接子窗口。

您的布局应如下所示:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/scroll_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />
</ScrollView>

然后,您可以在代码中将TextView添加到LinearLayout

LinearLayout scrollContainer = (LinearLayout) findViewById(R.id.scroll_container);
scrollContainer.addView(tv);

你应该看一下LogCat。它可能会告诉你:

java.lang.IllegalStateException: ScrollView can host only one direct child

来自ScrollView:245 source code

@Override
public void addView(View child) {
    if (getChildCount() > 0) {
        throw new IllegalStateException("ScrollView can host only one direct child");
    }

    super.addView(child);
}

修改(见第三条评论)
当你mainLinear.addView(tv1);时会发生这种情况。 您可以在Android Studio布局res/layout/scroll_test.xml中的Preview中对其进行测试。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:layout_margin="10dp"
    android:orientation="vertical"
    tools:context="me.ibtehaz.greapp.showWord"
    android:id="@+id/mainLinear">
    <TextView android:text="Word: Monkey Kaka"
        android:textSize="25dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/t1"
        android:textColor="#000000"/>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/ScrollView">
        <LinearLayout
            android:id="@+id/scroll_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />
    </ScrollView>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Test 1" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Test 2" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Test 3" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Test 4" />
</LinearLayout>

现在,如果您将ScrollView布局高度设置为wrap_content,您会看到显示4 TextView个。{/ p>

这更像是UI设计问题,而不是代码问题。

答案 1 :(得分:0)

我明白了。在所有情况下,我都给出了错误的hexValue作为背景。我从ViewGroup将布局参数更改为线性(我不知道为什么,在教程中找到它)。最后我使用了一个TextView数组,而不是重复创建一个对象。

感谢您的帮助:)

    TextView t1 = (TextView)findViewById(R.id.t1);
    LinearLayout mainLinear = (LinearLayout)findViewById(R.id.mainLinear);

    try {
        LinearLayout linear = (LinearLayout)findViewById(R.id.scroll_container);
        linear.setBackgroundColor(0xABCDEFFE);

        TextView []tv = new TextView[10];

        for (int i = 0; i < 10; i++) {

            tv[i] = new TextView(getApplicationContext());

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
            );
            tv[i].setLayoutParams(params);

            tv[i].setTextSize(25);
            tv[i].setId(i);
            tv[i].setTextColor(Color.BLACK);
            tv[i].setPadding(10, 10, 10, 10);
            tv[i].setBackgroundColor(Color.WHITE);
            tv[i].setText("I am serial 4 " + i + "\n" + "is there something more");
            linear.addView(tv[i]);
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
        t1.setText(value+"<><>"+e);
    }
相关问题