【问题标题】:unable to make the ScrollView and textView (added programmatically) visible in Android无法使 ScrollView 和 textView(以编程方式添加)在 Android 中可见
【发布时间】:2015-04-19 12:29:21
【问题描述】:

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

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

更新 2 我已将滚动视图的高度设为 WRAP_CONTENT,并在其下方添加了其他四个 TextView。现在,正在加载的应用程序显示顶部 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>

感谢您的帮助。

【问题讨论】:

    标签: java android xml android-layout


    【解决方案1】:

    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>
    

    然后,您可以从您的代码中将 TextViews 添加到 LinearLayout 中:

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

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

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

    来自ScrollView:245source 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 布局 Preview 中的 res/layout/scroll_test.xml 中对其进行测试。

    <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 个TextViews 出现。

    与其说是代码问题,不如说是 UI 设计问题。

    【讨论】:

    • mainLinear.addView(tv1); 可能无法“工作”。由于ScrollView 的高度设置为match_parentScrollView 将填充第一个TextView@+id/t1))留下的空间。您在ScrollView 下方添加的任何内容都将添加到屏幕之外。
    • 通过在 ScrollView 下方添加这些 TextViews 来尝试做什么?
    • 我做了你在上面的 XML 中写的东西,现在 UI 显示出一个考虑的差距,这表明有一个 ScrollView 但里面没有文本。
    • 最后一个 XML 不是“你应该做的”。它在这里向您展示ScrollView 上的match_parent 将填满整个屏幕,而wrap_content 只会使ScollView 无法使用。这绝对是一个 UI 设计问题
    • 我会尝试显示数据。我将从文件或服务器加载 JSON 数据并将它们加载到页面中。我的队友和朋友有 20 个布局,用于 20 个不同的文件,我试图将它们减少到 6 个,其中所有文件都将加载到一个布局中,具体取决于单击哪个按钮,然后获取 Intent 数据。
    【解决方案2】:

    我想通了。在所有情况下,我都为背景提供了错误的 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);
        }
    

    【讨论】:

      猜你喜欢
      • 2015-11-14
      • 1970-01-01
      • 2014-11-21
      • 1970-01-01
      • 1970-01-01
      • 2015-04-25
      • 1970-01-01
      • 2011-09-20
      相关资源
      最近更新 更多