2013-09-07 4 views
24

У меня есть относительный макет с несколькими «изображениями», и когда я поворачиваюсь по экрану, изображения становятся неупорядоченными ... поэтому я решил обернуть его в прокрутку ... но просмотр прокрутки не " т работы !!! Может ли кто-нибудь помочь мне в этом? Я знаю, что правильный путь - создать gridview или listview ... но поскольку у меня были некоторые вопросы, и никто не ответил мне в stackoverfloa, я решил пойти этим путем. вот мой XML-код:Относительная компоновка в ScrollView

<?xml version="1.0" encoding="utf-8"?> 


<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/ScrollView01" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true" 
    android:scrollbars="none" > 


<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 


    <Button 
     android:id="@+id/continuePizza" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:text="continue" /> 

    <Button 
     android:id="@+id/finishP" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/continuePizza" 
     android:layout_alignParentLeft="true" 
     android:text="finish" /> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="16dp" 
     android:src="@drawable/download" /> 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/imageView1" 
     android:layout_marginLeft="18dp" 
     android:layout_toRightOf="@+id/imageView1" 
     android:src="@drawable/download" /> 

    <ImageView 
     android:id="@+id/imageView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/imageView1" 
     android:layout_marginTop="18dp" 
     android:src="@drawable/download" /> 

    <ImageView 
     android:id="@+id/imageView5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/imageView3" 
     android:layout_centerVertical="true" 
     android:src="@drawable/download" /> 

    <ImageView 
     android:id="@+id/imageView6" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/imageView3" 
     android:src="@drawable/download" /> 

    <ImageView 
     android:id="@+id/imageView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/imageView5" 
     android:layout_marginTop="17dp" 
     android:src="@drawable/download" /> 

    <ImageView 
     android:id="@+id/imageView7" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/imageView2" 
     android:layout_alignTop="@+id/imageView5" 
     android:layout_marginLeft="10dp" 
     android:src="@drawable/download" /> 
    <ImageView 
     android:id="@+id/imageView8" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/imageView7" 
     android:layout_alignTop="@+id/imageView3" 
     android:src="@drawable/download" /> 

</RelativeLayout> 

</ScrollView> 

ответ

19

я испытал то же проблемы с относительным макетом в Scroll View, чтобы преодолеть это я обернут вокруг моего относительного расположения с линейной планировкой, попробуйте, как это, а также удалить ориентацию от относительного расположения

<?xml version="1.0" encoding="utf-8"?> 


<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/ScrollView01" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fillViewport="true" 
android:scrollbars="none" > 

<LinearLayout android:layout_width="match_parent" 
android:layout_height="match_parent"> 


<RelativeLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
> 


<Button 
    android:id="@+id/continuePizza" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:text="continue" /> 

<Button 
    android:id="@+id/finishP" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/continuePizza" 
    android:layout_alignParentLeft="true" 
    android:text="finish" /> 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginTop="16dp" 
    android:src="@drawable/download" /> 

<ImageView 
    android:id="@+id/imageView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/imageView1" 
    android:layout_marginLeft="18dp" 
    android:layout_toRightOf="@+id/imageView1" 
    android:src="@drawable/download" /> 

<ImageView 
    android:id="@+id/imageView3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/imageView1" 
    android:layout_marginTop="18dp" 
    android:src="@drawable/download" /> 

<ImageView 
    android:id="@+id/imageView5" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/imageView3" 
    android:layout_centerVertical="true" 
    android:src="@drawable/download" /> 

<ImageView 
    android:id="@+id/imageView6" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/imageView3" 
    android:src="@drawable/download" /> 

<ImageView 
    android:id="@+id/imageView4" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/imageView5" 
    android:layout_marginTop="17dp" 
    android:src="@drawable/download" /> 

<ImageView 
    android:id="@+id/imageView7" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/imageView2" 
    android:layout_alignTop="@+id/imageView5" 
    android:layout_marginLeft="10dp" 
    android:src="@drawable/download" /> 
<ImageView 
    android:id="@+id/imageView8" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/imageView7" 
    android:layout_alignTop="@+id/imageView3" 
    android:src="@drawable/download" /> 

</RelativeLayout> 
</LinearLayout> 
</ScrollView> 
+0

Я сделал свое решение и столкнулся так много ошибок! даже активность не появляется таким образом! :( –

+0

whats the error – Ravi

+0

Я хотел скопировать ошибки, и поэтому запустите приложение еще раз ... смешно, что в настоящее время нет никакой ошибки, но нет никакой разницы в проблеме! У меня есть эта проблема ... –

-4

Я мог бы решить мой вопрос с помощью дорогого Рави и небольшим изменением бит: код идет так:

<?xml version="1.0" encoding="utf-8"?> 


<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/ScrollView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:scrollbars="none" > 



     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="427dp" > 



<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="548dp" > 




    <Button 
     android:id="@+id/continuePizza" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:text="continue" /> 

    <Button 
     android:id="@+id/finishP" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/continuePizza" 
     android:layout_alignParentLeft="true" 
     android:text="finish" /> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="16dp" 
     android:src="@drawable/download" /> 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/imageView1" 
     android:layout_marginLeft="18dp" 
     android:layout_toRightOf="@+id/imageView1" 
     android:src="@drawable/download" /> 

    <ImageView 
     android:id="@+id/imageView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/imageView1" 
     android:layout_marginTop="18dp" 
     android:src="@drawable/download" /> 

    <ImageView 
     android:id="@+id/imageView5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/imageView3" 
     android:layout_centerVertical="true" 
     android:src="@drawable/download" /> 

    <ImageView 
     android:id="@+id/imageView6" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/imageView3" 
     android:src="@drawable/download" /> 

    <ImageView 
     android:id="@+id/imageView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/imageView5" 
     android:layout_marginTop="17dp" 
     android:src="@drawable/download" /> 

    <ImageView 
     android:id="@+id/imageView7" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/imageView2" 
     android:layout_alignTop="@+id/imageView5" 
     android:layout_marginLeft="10dp" 
     android:src="@drawable/download" /> 
    <ImageView 
     android:id="@+id/imageView8" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/imageView7" 
     android:layout_alignTop="@+id/imageView3" 
     android:src="@drawable/download" /> 

</RelativeLayout> 
</LinearLayout> 
</ScrollView> 
+2

Я не думаю, что жесткое кодирование размеров высоты - это путь. –

1

Когда вы используете ScrollView, вы должны знать, что вы можете прокручивать, когда высота ScrollView меньше высоты дочернего элемента ScrollView.
Чтобы решить эту проблему, вы можете определить Height LinearLayout для match_parent, который является единственным дочерним элементом ScrollView. Затем определите высоту ScrollView в коде Java, например scrollView.getChildAt(0).getHeight() - 1;, чтобы убедиться, что высота ScrollView меньше высоты дочернего элемента ScrollView.
Для вашего собственного ответа, это не очень хороший способ использовать магические числа в вашем коде, например 427dp и 548dp.

22

простое решение, просто добавьте андроида: fillViewport = «истина» в Scrollview как:

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true"> 

<RelativeLayout ....... /> 

</ScrollView> 
+0

Это сделало трюк! Благодаря! :) – Marlon

Смежные вопросы