Android 能给我一个有很多相对位置的滚动视图吗
我正在写一个活动,上面有一个线性布局,下半部分有一个滚动视图。scrollView在运行时可以获得多达30个relativeLayouts。这可能吗? 我已尝试使用以下代码,但出现错误:Android 能给我一个有很多相对位置的滚动视图吗,android,scrollview,Android,Scrollview,我正在写一个活动,上面有一个线性布局,下半部分有一个滚动视图。scrollView在运行时可以获得多达30个relativeLayouts。这可能吗? 我已尝试使用以下代码,但出现错误: public void onFocusChange(View v, boolean hasFocus) { // TODO Auto-generated method stub if(hasFocus==false){ switch(v.getId()){ case 6:
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus==false){
switch(v.getId()){
case 6:
main[no]=new RelativeLayout(this);
mainParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
main[no].setLayoutParams(mainParams);
mainLayout.addView(main[no]);
svParams=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
sv.setLayoutParams(svParams);
items[no]=new EditText(this);
rates[no]=new EditText(this);
quants[no]=new EditText(this);
items[no].setHint("Enter item name");
rates[no].setHint("Rate");
quants[no].setHint("Quantity");
RelativeLayout.LayoutParams etParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams rateParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams quantParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
items[no].setId(id++);
quantParams.addRule(RelativeLayout.RIGHT_OF, (id-1));
quants[no].setId(id++);
pos=id;
rateParams.addRule(RelativeLayout.RIGHT_OF, (id-1));
rates[no].setId(id++);
etParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
items[no].setLayoutParams(etParams);
rates[no].setLayoutParams(rateParams);
quants[no].setLayoutParams(quantParams);
rates[no].setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
quants[no].setInputType(InputType.TYPE_CLASS_NUMBER);
main[no].addView(items[no]);
main[no].addView(rates[no]);
main[no].addView(quants[no]);
quants[no].setOnFocusChangeListener(this);
sv.addView(main[no]);
no++;
break;
每个相对布局包含3个彼此相邻的编辑文本。ScrollView只能有一个子项。添加LinearLayout,它将包含所有RelativeLayout,并将LinearLayout添加到ScrollView
<ScrollView ...
<LinearLayout ...
<RelativeLayout1 ... />
<RelativeLayout2 ... />
</LinearLayout>
</ScrollView>
ScrollView只能有一个子项。添加LinearLayout,它将包含所有RelativeLayout,并将LinearLayout添加到ScrollView
<ScrollView ...
<LinearLayout ...
<RelativeLayout1 ... />
<RelativeLayout2 ... />
</LinearLayout>
</ScrollView>
如果您想在一个布局中添加那么多视图,我建议使用ListView,因为适配器将处理滚动和重用视图,因为在一个布局中有30个布局和3个EditExt的内存效率不高
但是如果你想使用滚动视图
ScrollView只能有一个直接子级。
所以你可以吃这样的东西
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!--Add your layouts here Like this -->
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</LinearLayout>
</ScrollView>
如果您想在一个版面中添加那么多视图,我建议使用ListView,因为适配器将处理滚动和重用视图,因为在一个版面中有30个版面和3个EditExt的内存效率不高
但是如果你想使用滚动视图
ScrollView只能有一个直接子级。
所以你可以吃这样的东西
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!--Add your layouts here Like this -->
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</LinearLayout>
</ScrollView>
对于此U,请使用布局充气机,如
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/main_layout_id">
</LinearLayout >
</ScrollView>
就这样 为此,请使用布局充气机,如
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/main_layout_id">
</LinearLayout >
</ScrollView>
就这样 不使用ListView的任何原因?我是否可以使用ListView以与现在相同的方式放置EditText?它会给您带来什么错误?nullPointerException@sv.addView(main[no]);不使用ListView的任何原因?我是否可以使用ListView以与现在相同的方式放置EditText?它会给您带来什么错误?nullPointerException@sv.addView(main[no]);在代码中,我尝试动态添加EditText。所以即使我像你说的那样做,它会起作用吗。例如,如果用户决定输入更多数据,那么当他接近屏幕末端时,EditText会自动在屏幕中移动吗scrollView@kayveesin如果要在何时使用ListView,则需要一个自定义适配器来填充ListView小部件。此自定义适配器将要求您重写getView方法,在该方法中,您将能够管理ListView显示的每个视图,并且在那里您将能够动态添加EditTextscan i use a tableLayout??哪一个是容易编程的,同时对资源的负载最小runtime@kayveesin如果使用ListView,程序上的最小负载(资源没有任何负载)将是。我不知道在使用RelativeLayouts或TableLayout的情况下,应用程序上的负载之间有什么区别,但我假设是相同的。ListView会创建几行来填充屏幕,当您滚动ListView时,它会自动创建重用(在旧数据上填充新数据)旧数据的视图ones@kayveesin但是,如果您确信没有太多的数据要显示,那么您可以将ScrollView与Relativelayouts或TableLayout或其他任何内容一起使用。这完全取决于你。试着看看如果你在代码中使用TableLayoutAs,你的应用程序是否会变慢。我正在尝试动态添加EditText。所以即使我像你说的那样做,它会起作用吗。例如,如果用户决定输入更多数据,那么当他接近屏幕末端时,EditText会自动在屏幕中移动吗scrollView@kayveesin如果要在何时使用ListView,则需要一个自定义适配器来填充ListView小部件。此自定义适配器将要求您重写getView方法,在该方法中,您将能够管理ListView显示的每个视图,并且在那里您将能够动态添加EditTextscan i use a tableLayout??哪一个是容易编程的,同时对资源的负载最小runtime@kayveesin如果使用ListView,程序上的最小负载(资源没有任何负载)将是。我不知道在使用RelativeLayouts或TableLayout的情况下,应用程序上的负载之间有什么区别,但我假设是相同的。ListView会创建几行来填充屏幕,当您滚动ListView时,它会自动创建重用(在旧数据上填充新数据)旧数据的视图ones@kayveesin但是,如果您确信没有太多的数据要显示,那么您可以将ScrollView与Relativelayouts或TableLayout或其他任何内容一起使用。这完全取决于你。如果使用TableLayout,请尝试查看应用程序是否会变慢。您可以传入listView适配器中要使用的任何布局,但处理已插入EditText中30行的值可能有点困难:/well您可以传入listView适配器中要用的任何布局,但可能是一个错误处理已插入EditText中30行的值有点困难:/