【问题标题】:Scroll View doesn't scroll in layout weights滚动视图不滚动布局权重
【发布时间】:2021-03-09 21:57:06
【问题描述】:

我想构建一个响应式设计的 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>

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

【问题讨论】:

    标签: java android xml android-studio 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多