我是Android编程的初学者
我添加了 LinearLayout 和 ScrollView。我已经用许多长 TextView 填充了 LinearLayout,以确保它足够长,能够滚动。我的问题是,这没有滚动,也不知道为什么。代码如下:
<!-- short version -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="false"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scrollbars="vertical"
android:id="@+id/description">
</LinearLayout>
</ScrollView>
<!-- full version -->
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/frame_id"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/relative_id"
android:gravity="top">
<androidx.media3.ui.PlayerView
android:id="@+id/playerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:keepScreenOn="true"
/>
<ImageButton
android:id="@+id/fullScreenBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/fullscreen_icon"
/>
<ImageView
android:id="@+id/loading_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/loading"
android:visibility="gone"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="false"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scrollbars="vertical"
android:id="@+id/description">
</LinearLayout>
</ScrollView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
我是 Android 新手,请给我一个提示。
非常感谢。 迈克
它不会滚动,因为视图组内没有任何子级
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="false"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scrollbars="vertical"
android:id="@+id/description">
</LinearLayout>
</ScrollView>
为了实现你的目标,请将孩子移动到滚动视图中
像这样
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/frame_id"
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/relative_id"
android:gravity="top">
<androidx.media3.ui.PlayerView
android:id="@+id/playerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:keepScreenOn="true"
/>
<ImageButton
android:id="@+id/fullScreenBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/fullscreen_icon"
/>
<ImageView
android:id="@+id/loading_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/loading"
android:visibility="gone"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scrollbars="vertical"
android:id="@+id/description">
<!-- Additional views can be added here -->
</LinearLayout>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>