Android - 使用LinearLayout创建一个可滚动的ScrollView

4

我想创建一个ScrollView,并在其中放置一个LinearLayout。该线性布局包含6个View,这些View的背景分别是CYAN、BLUE、CYAN、BLUE等。以下是代码:

public class TouchActivity extends Activity
{
    TouchedView TouchView;

    public void onCreate(Bundle icicle) 
    {
        super.onCreate(icicle);

        TouchView = new TouchedView(this);
        TouchView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT) );

        //setContentView(TouchView);
        setContentView(TouchView.ViewLayout);
    }

    class TouchedView extends ScrollView
    {
        LinearLayout ViewLayout;
        ListElement Elem1;
        ListElement Elem2;
        ListElement Elem3;
        ListElement Elem4;
        ListElement Elem5;
        ListElement Elem6;


        public TouchedView(Context context) 
        {
            super(context);

            ViewLayout = new TableLayout(TouchActivity.this);
            ViewLayout.setOrientation(LinearLayout.VERTICAL);

            Elem1 = new ListElement(TouchActivity.this , "CYAN");
            Elem1.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT , 100 ) );
            Elem2 = new ListElement(TouchActivity.this , "BLUE");
            Elem2.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT , 100 ) );
            Elem3 = new ListElement(TouchActivity.this , "CYAN");
            Elem3.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT , 100 ) );
            Elem4 = new ListElement(TouchActivity.this , "BLUE");
            Elem4.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT , 100 ) );
            Elem5 = new ListElement(TouchActivity.this , "CYAN");
            Elem5.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT , 100 ) );
            Elem6 = new ListElement(TouchActivity.this , "BLUE");
            Elem6.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT , 100 ) );

            ViewLayout.addView(Elem1);
            ViewLayout.addView(Elem2);
            ViewLayout.addView(Elem3);
            ViewLayout.addView(Elem4);
            ViewLayout.addView(Elem5);
            ViewLayout.addView(Elem6);

            setFillViewport(false);
            setContentView(ViewLayout);

        }

    }


    class ListElement extends View
    {
        public ListElement(Context context , String TypeName) 
        {
            super(context);

            if(TypeName.compareTo("CYAN") == 0) this.setBackgroundColor(Color.CYAN);

            if(TypeName.compareTo("BLUE") == 0) this.setBackgroundColor(Color.BLUE);    
        }
    }
}

结果是6个视图太大,无法包含在LinearLayout中: http://img513.imageshack.us/img513/5406/androidbadscrollview2.jpg 但如果我注释掉setContentView(TouchView.ViewLayout);并取消注释//setContentView(TouchView);,我的活动应该填充ScrollView而不是LinearLayout,但不幸的是我什么也看不到。
注意,ScrollView包含由setContentView(ViewLayout);设置的LinearLayout。
2个回答

5

请查看 http://www.vogella.com/articles/Android/article.html#gridlayout_scrollview

你可以声明任何子视图,而不仅限于 TextView。
重要的是你的 ScrollView 只有一个子视图(即一个 LinearLayout)。
此外,尽可能多地在 XML 中编写布局代码是非常重要的!

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/LinearLayout01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is a header">
        </TextView>

    </LinearLayout>

</ScrollView>

感谢@Thomas K的帮助!这个方法肯定有效,但我正在尝试在没有XML文件的情况下制作GUI,除非我想声明一个活动或添加权限。我想遵循你的建议,但XML需要学习另一种语言的语法。那么我为什么要这样做呢?如果需要手动工作,我更愿意使用众所周知的Java,并将XML留给自动设计工具,如DroidDraw和其他工具。 - Bemipefe

1

我主要犯了两个错误:

第一个错误是传递了TableLayout参数而不是LinearLayout参数,我的意思是:

Elem1.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT , 100 ) );

其次,扩展ScrollView似乎不是一个好主意。因此,创建一个带有LineraLayaout的简单ScrollView对象是解决方案。

这里是可行的代码

public class TouchActivity extends Activity
{

    public void onCreate(Bundle icicle) 
    {
        super.onCreate(icicle);

        ScrollView MainView;
        LinearLayout ViewLayout;

        ListElement Elem1;
        ListElement Elem2;
        ListElement Elem3;
        ListElement Elem4;
        ListElement Elem5;
        ListElement Elem6;

        MainView = new ScrollView(this); 

        ViewLayout = new LinearLayout(this);
        ViewLayout.setOrientation(LinearLayout.VERTICAL);
        ViewLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT) );

        Elem1 = new ListElement(this , "CYAN");
        Elem1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT , 100 ) );
        Elem2 = new ListElement(this , "BLUE");
        Elem2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT , 100 ) );
        Elem3 = new ListElement(this , "CYAN");
        Elem3.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT , 100 ) );
        Elem4 = new ListElement(this , "BLUE");
        Elem4.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT , 100 ) );
        Elem5 = new ListElement(this , "CYAN");
        Elem5.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT , 100 ) );
        Elem6 = new ListElement(this , "BLUE");
        Elem6.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT , 100 ) );

        ViewLayout.addView(Elem1);
        ViewLayout.addView(Elem2);
        ViewLayout.addView(Elem3);
        ViewLayout.addView(Elem4);
        ViewLayout.addView(Elem5);
        ViewLayout.addView(Elem6);    

        ViewLayout.requestLayout();
        MainView.addView(ViewLayout);
        setContentView(MainView);

    }

    class ListElement extends View
    {
        public ListElement(Context context , String TypeName) 
        {
            super(context);

            if(TypeName.compareTo("CYAN") == 0) this.setBackgroundColor(Color.CYAN);

            if(TypeName.compareTo("BLUE") == 0) this.setBackgroundColor(Color.BLUE);    
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接