滚动视图不在布局权重中滚动

jfewjypa  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(320)

我想构建一个具有响应性设计的ui,所以我使用布局权重。似乎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>

任何帮助都将不胜感激,谢谢!

bnlyeluc

bnlyeluc1#

嵌套权重不利于性能。但如果你非常需要这个,你需要删除固定高度的按钮,并需要设置重量按重量总和和高度应为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>

相关问题