Як зробити так, щоб мій макет міг прокручувати вниз?


88

Я не можу прокрутити екран вниз, щоб переглянути дані в розділі "Відповідає:". Як я можу зробити макет прокручуваним?

текст заміщення

Відповіді:


200

Просто оберніть все це всередині ScrollView:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <!-- Here you put the rest of your current view-->
</ScrollView>

Як сказав Девід Хедлунд, ScrollView може містити лише один предмет ... тож, якщо у вас було щось подібне:

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

Ви повинні змінити його на:

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

15
+1. коротке зауваження: a ScrollViewможе містити лише одну дитину, тому, якщо у вас є багато переглядів, вам потрібно обернути їх в одну групу переглядів (скажімо a LinearLayout)
Девід Хедлунд,

1
Дякую @David Hedlund
Prateek Raj

як виправити мою проблему , будь ласка , допоможіть мені stackoverflow.com/questions/38588702 / ...
Karthi

40

Для використання режиму прокрутки разом із відносним макетом:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"> <!--IMPORTANT otherwise backgrnd img. will not fill the whole screen -->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        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:background="@drawable/background_image"
    >

    <!-- Bla Bla Bla i.e. Your Textviews/Buttons etc. -->
    </RelativeLayout>
</ScrollView>

1
Що таке тег області огляду
Ruchir Baronia

4

Просто оберніть все це всередині ScrollView

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

<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"
    tools:context="com.ruatech.sanikamal.justjava.MainActivity">
<!-- Here you put the rest of your current view-->
</ScrollView>

0

Якщо ви навіть не отримали прокрутки після того, як написали вище ...

Встановіть android:layout_height="250dp"або ви можете сказати, xdpде xможе бути будь-яке числове значення.


0

Так, це дуже просто. Просто помістіть свій код всередину цього:

<androidx.core.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="match_parent">

//YOUR CODE

</androidx.core.widget.NestedScrollView>
Використовуючи наш веб-сайт, ви визнаєте, що прочитали та зрозуміли наші Політику щодо файлів cookie та Політику конфіденційності.
Licensed under cc by-sa 3.0 with attribution required.