java Android - 使用 LinearLayout 制作 ScrollView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10978405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - Make a ScrollView with LinearLayout
提问by Bemipefe
I want to make a ScrollView with a LinearLayout inside. The linear layout contains 6 View that have the background CYAN, BLUE, CYAN, BLUE etc... This is the code:
我想在里面制作一个带有 LinearLayout 的 ScrollView。线性布局包含 6 个具有青色、蓝色、青色、蓝色等背景的视图......这是代码:
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);
}
}
}
The result is that the 6 view are too big to be contained in the LinearLayout:
结果是 6 个视图太大,无法包含在 LinearLayout 中:
http://img513.imageshack.us/img513/5406/androidbadscrollview2.jpg
http://img513.imageshack.us/img513/5406/androidbadscrollview2.jpg
But if I comment setContentView(TouchView.ViewLayout);
and I uncomment //setContentView(TouchView);
my activity should be filled with the ScrollView instead of the LinearLayout
but unfortunately I can't see anything.
但是,如果我发表评论setContentView(TouchView.ViewLayout);
并取消评论,//setContentView(TouchView);
我的活动应该用 ScrollView 而不是 ScrollView 填充,LinearLayout
但不幸的是我什么也看不到。
Note that the ScrollView
contains the LinearLayout
that is set by setContentView(ViewLayout);
请注意,ScrollView
包含由LinearLayout
设置的setContentView(ViewLayout);
回答by Thkru
See http://www.vogella.com/articles/Android/article.html#gridlayout_scrollview
请参阅http://www.vogella.com/articles/Android/article.html#gridlayout_scrollview
Instead of the TextView you could declare any childs u want.
It's just important that your scrollview does only have ONE chield (i.e. a linearLayout).
Furthermore you should ALWAYS code as much layout in xml as possible!
您可以声明您想要的任何孩子,而不是 TextView。
重要的是您的滚动视图只有一个子域(即线性布局)。
此外,您应该始终在 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>
回答by Bemipefe
I have done mainly two error:
我主要做了两个错误:
The first is to pass a TableLayout parameter instead of LinearLayout parameter, i mean:
第一个是传递一个 TableLayout 参数而不是 LinearLayout 参数,我的意思是:
Elem1.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT , 100 ) );
And second to extend a ScrollView seem to be not a good thing. So creating a simple ScrollView object with a LineraLayaout inside is the solution.
其次扩展一个 ScrollView 似乎不是一件好事。因此,解决方案是创建一个内部带有 LineraLayaout 的简单 ScrollView 对象。
Here is the working code:
这是工作代码:
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);
}
}
}