[英]Android - Make a ScrollView with LinearLayout
我想在里面创建一个带有LinearLayout的ScrollView。 线性布局包含6个视图,背景为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
setContentView(ViewLayout);
请参阅http://www.vogella.com/articles/Android/article.html#gridlayout_scrollview
您可以声明您想要的任何子项,而不是TextView。
滚动视图只有一个chield(即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>
我主要做了两个错误:
第一个是传递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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.