2013-11-12 1 views
1

Я хочу добавить scrollview в свое приложение. Приложение содержит два линейных макета, в которых я динамически добавляю данные. Я хочу прокрутить весь экран, чтобы он отображал все данные. Когда я пытаюсь добавить его, появляется ошибка: «В представлении прокрутки может быть только один ребенок». Как добавить Scrollview в layout.xml?Как добавить ScrollView для этого макета?

Вот мой layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:id="@+id/myapp" 
    tools:context=".MainActivity" > 

    <ImageView 
     android:id="@+id/my_logo" 
     android:layout_width="fill_parent" 
     android:layout_height="50dp" 
     android:src="@drawable/my_logo" 
     > 
    </ImageView> 

    <TextView 
     android:id="@+id/label1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/label1" 
     android:layout_below="@id/my_logo" /> 

    <Spinner 
     android:id="@+id/spinner1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/label1"/> 

    <TextView 
     android:id="@+id/label2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/spinner1" 
     android:text="@string/label2" /> 

    <Spinner 
     android:id="@+id/spinner2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/label2"/> 

    <TextView 
     android:id="@+id/label3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/spinner2" 
     android:text="@string/label3" /> 


    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:id="@+id/list" 
     android:layout_below="@id/label3" 
     > 

    </LinearLayout> 

    <Button 
     android:id="@+id/submit_button" 
     android:layout_below="@id/list2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/submit_button"/> 

    <TextView 
     android:id="@+id/label4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/submit_button" 
     android:text="@string/label4" /> 

    <LinearLayout 
    android:id="@+id/my_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:layout_below="@id/label4" /> 

</RelativeLayout> 
+0

Просто положи Относительное расположение внутри Scrollview он будет делать трюк – krishna

+0

Как добавить колонтитул изображение в том же макете? Когда я добавил изображение нижнего колонтитула, он перекрывается с динамическими данными. – tushAR

ответ

1

ScrollView очень похож на FrameLayout. Они оба работают лучше, когда есть только один вид. Сделайте ScrollView родительским Просмотр и поместите все внутри.

+1

Scroll View не работает лучше, он работает только тогда, когда есть только «ОДИН» прямой ребенок. –

0

Как он говорит, Scrollview может иметь только один ребенок, просто поставить макет, как ребенок, и все ваше содержание в этой схеме.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity"> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/myapp" > 
    <ImageView 
     android:id="@+id/my_logo" 
     android:layout_width="fill_parent" 
     android:layout_height="50dp" 
     android:src="@drawable/my_logo" 
     > 
    </ImageView> 

    <TextView 
     android:id="@+id/label1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/label1" 
     android:layout_below="@id/my_logo" /> 

    <Spinner 
     android:id="@+id/spinner1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/label1"/> 

    <TextView 
     android:id="@+id/label2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/spinner1" 
     android:text="@string/label2" /> 

    <Spinner 
     android:id="@+id/spinner2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/label2"/> 

    <TextView 
     android:id="@+id/label3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/spinner2" 
     android:text="@string/label3" /> 


    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:id="@+id/list" 
     android:layout_below="@id/label3" 
     > 

    </LinearLayout> 

    <Button 
     android:id="@+id/submit_button" 
     android:layout_below="@id/list2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/submit_button"/> 

    <TextView 
     android:id="@+id/label4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/submit_button" 
     android:text="@string/label4" /> 

    <LinearLayout 
    android:id="@+id/my_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:layout_below="@id/label4" /> 
</RelativeLayout> 
</ScrollView> 
0

Просто положите Относительное расположение внутри Scrollview это будет делать трюк

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools"> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:id="@+id/myapp" 
    tools:context=".MainActivity" > 

    <ImageView 
     android:id="@+id/my_logo" 
     android:layout_width="fill_parent" 
     android:layout_height="50dp" 
     > 
    </ImageView> 

    <TextView 
     android:id="@+id/label1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="ss" 
     android:layout_below="@id/my_logo" /> 

    <Spinner 
     android:id="@+id/spinner1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/label1"/> 

    <TextView 
     android:id="@+id/label2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/spinner1" 
     android:text="ss" /> 

    <Spinner 
     android:id="@+id/spinner2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/label2"/> 

    <TextView 
     android:id="@+id/label3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/spinner2" 
     android:text="ss" /> 


    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:id="@+id/list" 
     android:layout_below="@id/label3" 
     > 

    </LinearLayout> 

    <Button 
     android:id="@+id/submit_button" 
     android:layout_below="@id/list" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="sss"/> 

    <TextView 
     android:id="@+id/label4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/submit_button" 
     android:text="ssss" /> 

    <LinearLayout 
    android:id="@+id/my_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:layout_below="@id/label4" /> 

</RelativeLayout> 
</ScrollView> 
0

Wrap весь макет внутри Relative Layout или FrameLayout , а затем добавить Scrollview ScrollView может иметь только один непосредственный элемент, как ребенок

<ScrollView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
<!--   yourLayout here --> 

    </RelativeLayout> 
</ScrollView 
Смежные вопросы