#java #android #xml #android-studio #scrollview
#java #Android #xml #android-studio #scrollview
Вопрос:
Я хочу создать пользовательский интерфейс с адаптивным дизайном, поэтому я использую веса макета. Похоже, что ScrollView не будет прокручиваться с весами. Я пробовал использовать ScrollView без весов, и прокрутка работает нормально. Интересно, есть ли способ использовать ScrollView с весами. Вот мой код:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:background="@color/colorAccent"
android:layout_weight="0.3"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:background="@color/colorPrimary"
android:layout_weight="0.7">
<Button
android:layout_width="match_parent"
android:layout_height="200dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="200dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="200dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="200dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="200dp"
android:text="Last Button"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
Любая помощь будет оценена, спасибо!
Ответ №1:
Вложенные веса плохо влияют на производительность. Но если вам это очень нужно, вам нужно удалить фиксированную высоту кнопок и установить вес в соответствии с weightsum, а высота должна быть 0. Внесите изменения, как показано ниже —
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:background="@color/colorAccent"
android:layout_weight="0.3"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:background="@color/colorPrimary"
android:layout_weight="0.7"
android:weightSum="5">
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Last Button"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>