Android 当有超过1个子元素时,使用java向scrollview添加元素
我正在尝试向scrollview添加元素, 此滚动视图内部具有垂直线性布局。 (我添加了另一个线性布局-Horizontal,内部将根据我指定的属性管理2个文本视图和一个图像) 我正在尝试将此内部布局的元素添加到我的滚动视图(id=insideLineLayout) (添加到scrollview的每个元素将包含这两个文本和一个图像) 因为scrollview只能有一个孩子,所以我无法做到这一点。 如果你能帮上忙,我会很感激的,这里似乎没什么用。 谢谢 我当前的xml代码:Android 当有超过1个子元素时,使用java向scrollview添加元素,android,android-scrollview,Android,Android Scrollview,我正在尝试向scrollview添加元素, 此滚动视图内部具有垂直线性布局。 (我添加了另一个线性布局-Horizontal,内部将根据我指定的属性管理2个文本视图和一个图像) 我正在尝试将此内部布局的元素添加到我的滚动视图(id=insideLineLayout) (添加到scrollview的每个元素将包含这两个文本和一个图像) 因为scrollview只能有一个孩子,所以我无法做到这一点。 如果你能帮上忙,我会很感激的,这里似乎没什么用。 谢谢 我当前的xml代码: <?xml ve
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/searchScreenOuterLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/catagoryselectedText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:text="selected catagory"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="12dp"
android:orientation="horizontal" >
<EditText
android:id="@+id/searchText"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp" />
<ImageButton
android:id="@+id/searchButton"
android:layout_width="50dp"
android:layout_height="40dp"
android:background="@drawable/search_icon" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/radiusText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:paddingTop="5dp"
android:text="Search Radius"
android:textAppearance="?android:attr/textAppearanceMedium" />
<SeekBar
android:id="@+id/radiusBar"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp" />
</LinearLayout>
<ScrollView
android:id="@+id/searchScrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/searchScrollViewLinear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingTop="10dp" >
<LinearLayout
android:id="@+id/insideLineLayout"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:orientation="horizontal"
android:weightSum="100"
>
<TextView
android:id="@+id/linesPrice"
android:layout_weight="20"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="tmp1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/linesInfo"
android:layout_weight="60"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="tmp2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/imageInfo"
android:layout_width="40dp"
android:layout_height="match_parent"
android:background="@drawable/tv_icon" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
考虑一下列表的长度,因为
ListView
已经在许多方面进行了优化,而常规滚动视图则没有。如果元素太多,可能会出现内存不足异常
为了向滚动视图
动态添加视图,首先需要在滚动视图
中获取容器视图(很可能是线性布局
),然后只需对添加的每个视图执行以下操作:
View view = inflater.inflate(R.layout.view_to_add, container, false); // this is used if you create from xml, if you make it in code you'd use the regular constructor.
// update the view content (view.findViewById(); etc.)
container.addView(view);
您可以制作
insideLineLayout
的多个副本,并将它们添加到searchScrollViewLinear
。但这看起来很像ListView…添加这些元素应该是动态的,因为在我得到它们之前,我不知道将添加多少元素以及它们的信息。(否则你是对的,我会照你的建议去做)@Roy有什么问题吗?以编程方式将视图添加到ScrollView内部的LinearLayout会导致错误吗+1给萨姆。