在ScrollView中添加LinearLayout xml

时间:2014-09-09 01:53:49

标签: android xml android-layout scrollview

我有一个带ScrollView的xml和一个LinearLayout作为ScrollView的子项。

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

    <ViewStub
        android:id="@+id/social_empty_stub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:inflatedId="@+id/social_empty_stub_inflted"
        android:layout="@layout/empty_screen"
        android:visibility="gone" />

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/activity_stream"
        android:background="#DCDCDCDC"
        android:fadingEdge="none" >

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

</RelativeLayout>

我还有另一个带有LinearLayout

的xml
<?xml version="1.0" encoding="utf-8"?>
<!-- List Item on activity stream -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/RelativeLayout_Activity_Streams"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:paddingTop="5dp" >

    <!-- Avatar -->
    <com.example.bb.ShaderImageView
            android:id="@+id/imageView_Avatar"
            android:layout_width="@dimen/social_avatar_size"
            android:layout_height="@dimen/social_avatar_size"
            android:layout_marginLeft="5dp"
            android:scaleType="fitXY" />

    <!-- Content -->
    <LinearLayout
            android:id="@+id/relativeLayout_Content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2dip"
            android:layout_marginRight="5dp"
            android:background="@drawable/news_browser_background_shape"
            android:orientation="vertical"
            android:padding="5dp" >

        <TextView
                android:id="@+id/textView_Name"
                style="@style/textview_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dip"
                android:maxLines="5"
                android:textStyle="bold" />

        <TextView
                android:id="@+id/textView_Message"
                style="@style/textview_content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dip"
                android:layout_marginTop="3dip"
                android:maxLines="5" />

        <TextView
                android:id="@+id/textview_temp_message"
                style="@style/textview_content"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dip"
                android:layout_marginTop="3dip"
                android:maxLines="5"
                android:visibility="gone" />

        <ViewStub
                android:id="@+id/attached_image_stub_activity"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="2dp"
                android:inflatedId="@+id/attached_image_stub_inflate"
                android:layout="@layout/activity_image_display_layout"
                android:padding="5dp"
                android:visibility="gone" />

        <TextView
                android:id="@+id/activity_comment_view"
                style="@style/textview_content"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dip"
                android:layout_marginTop="3dip"
                android:maxLines="5"
                android:visibility="gone" />

        <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="3dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="7dp"
                android:gravity="center_vertical">

            <ImageView
                    android:id="@+id/activity_image_type"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:contentDescription="@string/ImageDesc"
                    android:layout_marginLeft="5dp"
                    android:padding="2dp"
                    android:scaleType="fitXY" />

            <!-- Time text view -->
            <TextView
                    android:id="@+id/textView_Time"
                    style="@style/textview_time"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@id/activity_image_type"
                    android:layout_marginLeft="5dp"
                    android:layout_marginTop="5dp"
                    android:gravity="center_horizontal"
                    android:layout_marginRight="3dp" />

            <!-- Comment button -->
            <Button
                    android:id="@+id/button_Comment"
                    style="@style/textview_number"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:background="@drawable/social_activity_browser_comment_button_shape"
                    android:gravity="center_vertical"
                    android:padding="2dp" />

            <!-- Like button -->
            <Button
                    android:id="@+id/button_Like"
                    style="@style/textview_number"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toLeftOf="@id/button_Comment"
                    android:background="@drawable/social_activity_browser_like_button_shape"
                    android:gravity="center_vertical"
                    android:padding="2dp" />

        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

现在我想在第一个xml的ScrollView中以编程方式在第二个xml中添加LinearLayout。我正在使用以下代码

   setContentView(R.layout.activitybrowserview);  

   View view = LayoutInflater.from(context).inflate(R.layout.activitybrowserviewcell, null);
   LinearLayout ll = (LinearLayout) view.findViewById(R.id.RelativeLayout_Activity_Streams);
   ScrollView sv = (ScrollView) findViewById(R.id.activity_stream);
   sv.addView(ll);

我得到以下错误,我该如何避免。我不明白这个错误。

IllegalStateException“Scrollview只能托管一个直接子项”

4 个答案:

答案 0 :(得分:0)

在您的第一个XML文件中

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/activity_stream"
        android:background="#DCDCDCDC"
        android:fadingEdge="none" >

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

如果您在此处移除<LinearLayout>,则可以添加其他LinearLayout。您可以直接从XML文件中删除它,也可以以编程方式将其删除。

答案 1 :(得分:0)

Scrollview只能托管一个直接子项,scrollvie还有直接子项(android:id =“@ + id / activity_stream_wrap”),你应该用这个添加视图。

   setContentView(R.layout.activitybrowserview);  
   View view = LayoutInflater.from(context).inflate(R.layout.activitybrowserviewcell, null);
   LinearLayout ll = (LinearLayout) view.findViewById(R.id.RelativeLayout_Activity_Streams);
   LinearLayout parentLl = (LinearLayout ) findViewById(R.id.activity_stream_wrap);
   parentLl .addView(ll);

答案 2 :(得分:0)

只需将线性布局作为容器添加到当前的LinearLayout和要以实际方式添加的LinearLayout。我认为这应该可以解决你的问题。 ScrolView只能包含一个子节点,因为例外情况相同。

<ScrolView>
<LinearLayout> this will be the container
    <linearlayout> the one that is currently in the first xml file 
    </LinearLayout>
</LinearLayout>
</ScrolView>

希望这会有所帮助。

答案 3 :(得分:0)

 setContentView(R.layout.activitybrowserview);  

 View view = LayoutInflater.from(context).inflate(R.layout.activitybrowserviewcell, null);
 LinearLayout ll = (LinearLayout) view.findViewById(R.id.RelativeLayout_Activity_Streams);
 ScrollView sv = (ScrollView) findViewById(R.id.activity_stream);
 sv.removeAllViews();
 sv.addView(ll);