Android 安卓:只有一行是可见的

Android 安卓:只有一行是可见的,android,performance,android-layout,android-scrollview,Android,Performance,Android Layout,Android Scrollview,我有XML: <RelativeLayout android:id="@+id/IcdSearchMainPanel" android:layout_width="match_parent" android:layout_height="match_parent" > <ScrollView android:layout_width="match_parent"

我有XML:

   <RelativeLayout
        android:id="@+id/IcdSearchMainPanel"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:ignore="UselessParent" >

            <own_components.SearchOutput
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="top"
                android:orientation="vertical" >

                <!-- HERE ARE ADDED ROWS WITH RESULTS IN JAVA CODE (look into SearchOutput.java) -->

            </own_components.SearchOutput>
        </ScrollView>
    </RelativeLayout>

我有一个类SearchOutput.java(它扩展了LinearLayout)和这样一个方法(通常它会将一些图形组件添加到行中,然后将这一行添加到滚动视图中):

public void setResultOutput(ResultContainer结果)
{
对于(int i=0;i
问题:
无论
result
变量中有多少个元素,始终显示一行:

问题:
我做错了什么?在我看来,从逻辑上讲,一切都是好的。

另一个问题:这种方式有效吗?也许我应该以不同的方式添加结果?我这样问是因为可以有很多。一个ScrollView只能有一个子视图,所以将您的布局包装到另一个大布局中,并添加到ScrollView中


但正如评论中所建议的,使用ListView!!!这就是它的用途…尤其是当您有许多行要显示时。ListView将回收未使用的视图,并且肯定会产生比您的方法更好的性能。

Scroll view是一个使其自己的子视图可滚动的容器,因此,您必须将子视图添加到scrollview,然后将项目添加到该子视图。
无需掌握子项是什么,它可以是列表视图,也可以是线性布局,等等。

Replace
resultRow.setOrientation(LinearLayout.HORIZONTAL)带有
resultRow.setOrientation(LinearLayout.VERTICAL)我强烈建议使用列表视图。@Danil:这将导致只有标志位于顶部,“A00.0”位于标志下方,“A00.0”下方的文本位于“A00.0”下方。它不会使其他行出现。什么扩展了SearchOutput类?我忘了添加SearchOutput扩展了LinearLayout。事实上,这是一件(我希望)应该包装的东西。无论如何,若ScrollView包含多个元素,log将返回错误。我忘记添加SearchOutput扩展LinearLayout。事实上,这是一件(我希望)应该包装的东西。无论如何,如果ScrollView包含多个元素,log将返回error.COMMENT TO SOLUTION:我使用本教程学习如何使用ListView。现在一切都正常了。明智的选择,vogella的教程相当不错,我偶尔也会回到它们:)我已经在xml和代码中添加了它。你看到没有添加的东西了吗?可能我遗漏了什么?尝试双向滚动,如果可以看到其他行,尝试调整滚动视图大小
public void setResultOutput(ResultContainer result)
    {
        for (int i = 0; i < result.size(); i++)
        {
            LinearLayout resultRow = new LinearLayout(getContext());
            resultRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            resultRow.setOrientation(LinearLayout.HORIZONTAL);
            resultRow.setGravity(Gravity.CENTER_VERTICAL);
            if (i % result.getIterationStep() == 0)
            {
                resultRow.setPadding(0, 0, 0, 10);
            }
            addView(resultRow);

            ImageView langFlag = new ImageView(resultRow.getContext());
            langFlag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            try
            {
                Bitmap bitmap = BitmapFactory.decodeStream(MainClass.getAssetManager().open(Pathes.FLAGS_DIR + result.get(i)[0] + ".gif"));
                langFlag.setImageBitmap(bitmap);
                langFlag.setPadding(10, 0, 0, 0);
            }
            catch (IOException e)
            {
                Log.e("IMAGE_OPEN_EXC", e.toString());
            }
            resultRow.addView(langFlag);

            TextView number = new TextView(resultRow.getContext());
            number.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            number.setPadding(10, 0, 0, 0);
            number.setText(result.get(i)[1]);
            resultRow.addView(number);

            TextView text = new TextView(resultRow.getContext());
            text.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            text.setPadding(20, 0, 0, 0);
            text.setText(result.get(i)[2]);
            resultRow.addView(text);
        }
    }