Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android scrollview不适用于动态内容_Java_Android_Scrollview_Android Linearlayout_Android Relativelayout - Fatal编程技术网

Java Android scrollview不适用于动态内容

Java Android scrollview不适用于动态内容,java,android,scrollview,android-linearlayout,android-relativelayout,Java,Android,Scrollview,Android Linearlayout,Android Relativelayout,我正在根据从api获取的JSON数据动态创建表行和文本视图。目前数据显示正确,但我的scrollview无法正常工作。它不会滚动。我确实添加了一个滚动视图,但我想我在那里做错了什么 我的Xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_p

我正在根据从api获取的JSON数据动态创建表行和文本视图。目前数据显示正确,但我的scrollview无法正常工作。它不会滚动。我确实添加了一个滚动视图,但我想我在那里做错了什么

我的Xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ScrollView android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true"
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android">

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <TableLayout
                android:id="@+id/tlMarksTable"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

            </TableLayout>

        </LinearLayout>

    </ScrollView>

</RelativeLayout>

生成视图的部分如下所示:

jsonArray = new JSONArray(result);

TableRow tableRow = new TableRow(context);
setContentView(tableRow);
tableRow.setOrientation(LinearLayout.VERTICAL);

for(int i= 0; i < jsonArray.length(); i++) {
    JSONObject jsonobject = jsonArray.getJSONObject(i);
    String id         = jsonobject.getString("id");
    String content    = jsonobject.getString("content");

    TextView textView = new TextView(context);
    textView.setText(content);
    if (Build.VERSION.SDK_INT < 23) {
        textView.setTextAppearance(getApplicationContext(), R.style.boldText);
    } else {
        textView.setTextAppearance(R.style.boldText);
    }
    textView.setBackgroundResource(R.color.highlightedTextViewColor);
    textView.setPadding(10, 10, 10, 10);
    textView.setTextSize(getResources().getDimension(R.dimen.joke_content_font_size));
    tableRow.addView(textView);
}
          <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TableLayout
                android:id="@+id/tlMarksTable"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

            </TableLayout>

        </LinearLayout>
jsonArray=新的jsonArray(结果);
TableRow TableRow=新的TableRow(上下文);
setContentView(tableRow);
tableRow.setOrientation(LinearLayout.VERTICAL);
for(int i=0;i
内部组件的配置如下:

jsonArray = new JSONArray(result);

TableRow tableRow = new TableRow(context);
setContentView(tableRow);
tableRow.setOrientation(LinearLayout.VERTICAL);

for(int i= 0; i < jsonArray.length(); i++) {
    JSONObject jsonobject = jsonArray.getJSONObject(i);
    String id         = jsonobject.getString("id");
    String content    = jsonobject.getString("content");

    TextView textView = new TextView(context);
    textView.setText(content);
    if (Build.VERSION.SDK_INT < 23) {
        textView.setTextAppearance(getApplicationContext(), R.style.boldText);
    } else {
        textView.setTextAppearance(R.style.boldText);
    }
    textView.setBackgroundResource(R.color.highlightedTextViewColor);
    textView.setPadding(10, 10, 10, 10);
    textView.setTextSize(getResources().getDimension(R.dimen.joke_content_font_size));
    tableRow.addView(textView);
}
          <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TableLayout
                android:id="@+id/tlMarksTable"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

            </TableLayout>

        </LinearLayout>


顺便说一句,外部
相对布局
视图组没有太多功能我在这里看到的主要问题是,封装
表格布局的
线性布局
具有
android:layout\u height=“match\u parent”
。这意味着
滚动视图
的内部子级将与父级具有相同的高度,从而形成非滚动布局

为什么不尝试删除该
线性布局
,并将
表格布局
作为唯一的子项?请记住将该子对象的高度设置为
android:layout\u height=“wrap\u content”

顺便说一下,我建议使用
ListView
来显示大量数据


最后一件事:不需要声明xml名称空间(
xmlns:android=”http://schemas.android.com/apk/res/android“
)在每个布局声明中。在根元素上仅使用一个声明即可;)

为什么不使用回收视图?
您应该使用改装来使用WS

为什么不使用gridview?@tyczj抱歉,我在这方面没有太多经验,您能进一步解释一下您的意思吗?嗨,谢谢您的回答,我删除了外部的
RelativeLayout
,并用您提供的xml替换了xml,但我仍然无法滚动任何其他想法?ScrollView是您的顶级视图组吗?WS是什么意思?Web服务,API中的数据