【问题标题】:linear layout weight inside scrollview滚动视图内的线性布局权重
【发布时间】:2023-04-02 04:02:01
【问题描述】:

我正在尝试做的是这样的事情。

图片一

无论是哪个设备。它应该在屏幕上只显示四行,以后用户可以滚动查看其他行

我正在使用垂直线性布局权重来实现这一点,但它并没有按照我试图实现这一点的方式发生。当我添加更多行时,它会给我这样的屏幕

图片二

下面是我的布局 xml,它将屏幕作为图像之一,但添加更多行会提供图像两个视图

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/lightGray"
android:orientation="vertical">

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:padding="1dp"
                android:weightSum="4"

                >


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:orientation="horizontal"
                    android:weightSum="3">


                    <ImageView
                        android:id="@+id/abulation"
                        style="@style/icon"
                        android:background="@drawable/wudu_96" />

                    <ImageButton
                        android:id="@+id/masjid"
                        style="@style/icon"
                        android:background="@drawable/masjid_96" />


                    <ImageView
                        android:id="@+id/restroom"
                        style="@style/icon"
                        android:background="@drawable/restroom_96" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:orientation="horizontal"
                    android:weightSum="3">

                    <ImageView
                        android:id="@+id/dressing"
                        style="@style/icon"
                        android:background="@drawable/dressing_96" />

                    <ImageButton
                        style="@style/icon"
                        android:background="@drawable/icon_selector"
                        android:scaleType="fitXY" />


                    <ImageButton
                        android:id="@+id/Favourite"
                        style="@style/icon"
                        android:background="@drawable/favourite_96"
                        android:onClick="random"
                        android:scaleType="fitXY" />


                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:orientation="horizontal"
                    android:weightSum="3">

                    <ImageView
                        style="@style/icon"
                        android:background="@drawable/fear_96" />

                    <ImageView
                        style="@style/icon"
                        android:background="@drawable/fear_96" />

                    <ImageView
                        style="@style/icon"
                        android:background="@drawable/fear_96" />
                </LinearLayout>


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:orientation="horizontal"
                    android:weightSum="3">

                    <ImageView
                        style="@style/icon"
                        android:background="@drawable/fear_96" />

                    <ImageView
                        style="@style/icon"
                        android:background="@drawable/fear_96" />

                    <ImageView
                        style="@style/icon"
                        android:background="@drawable/fear_96" />
                </LinearLayout>

            </LinearLayout>



        </LinearLayout>
    </ScrollView>
  </android.support.v4.widget.NestedScrollView>
</LinearLayout>

我知道我在做错事来实现这一目标。请帮助我如何获得我想要的视图。

【问题讨论】:

  • 为什么不使用带有网格的回收站视图。?我认为这是实现这一目标的更好方法
  • 试试这个方法——在第一个线性布局中(android:weightSum="1.0")和里面的那些(android:layout_width="0dp" android:layout_weight="0.25")——见如果这给了你 4 个相等的行
  • 这些类别是静态的吗?
  • IMO,这不是 layout_weight 的问题,您需要找到可用的屏幕高度,将其除以 4,然后在运行时将该高度应用于每个 LinearLayout。跨度>
  • 垂直所以试试 (android:layout_height="0dp")

标签: android android-linearlayout android-scrollview


【解决方案1】:

试试这个。您已将视图包装在 ScrollView 中的另一个父 LinearLayout 中,这可能就是您的问题的原因。

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

    android:orientation="vertical">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/toolbar"
            android:fillViewport="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:weightSum="4">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:weightSum="3"
                    android:layout_height="0dp"
                    android:layout_weight="1">
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:weightSum="3"
                    android:layout_height="0dp"
                    android:layout_weight="1">
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:weightSum="3"
                    android:layout_height="0dp"
                    android:layout_weight="1">
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:weightSum="3"
                    android:layout_height="0dp"
                    android:layout_weight="1">
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                </LinearLayout>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:weightSum="4">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:weightSum="3"
                    android:layout_height="0dp"
                    android:layout_weight="1">
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:weightSum="3"
                    android:layout_height="0dp"
                    android:layout_weight="1">
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:weightSum="3"
                    android:layout_height="0dp"
                    android:layout_weight="1">
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:weightSum="3"
                    android:layout_height="0dp"
                    android:layout_weight="1">
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                </LinearLayout>
            </LinearLayout>
        </ScrollView>
    </android.support.v4.widget.NestedScrollView>
</LinearLayout>

【讨论】:

    【解决方案2】:

    试试这个。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/lightGray"
    android:orientation="vertical">
    
    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/toolbar"
            android:fillViewport="true">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:padding="1dp"
                    android:weightSum="4"
    
                    >
    
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1"
                        android:orientation="horizontal"
                        android:weightSum="3">
    
    
                        <ImageView
                            android:id="@+id/abulation"
                            android:layout_width="wrap_content"
                            android:layout_weight="1"
                            android:layout_height="match_parent"
                            android:background="#FF0F0F" />
    
                        <ImageButton
                            android:id="@+id/masjid"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:layout_width="wrap_content"
                            android:background="#D0060F" />
    
    
                        <ImageView
                            android:id="@+id/restroom"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:layout_width="wrap_content"
                            android:background="#1F0F0F" />
    
                    </LinearLayout>
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1"
                        android:orientation="horizontal"
                        android:weightSum="3">
    
                        <ImageView
                            android:id="@+id/dressing"
                            android:layout_width="wrap_content"
                            android:layout_weight="1"
                            android:layout_height="match_parent"
                            android:background="#FF0F0F" />
    
                        <ImageButton
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:layout_width="wrap_content"
                            android:background="#1F0F0F"
                            android:scaleType="fitXY" />
    
    
                        <ImageButton
                            android:id="@+id/Favourite"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:layout_width="wrap_content"
                            android:background="#D0060F"
                            android:onClick="random"
                            android:scaleType="fitXY" />
    
    
                    </LinearLayout>
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1"
                        android:orientation="horizontal"
                        android:weightSum="3">
    
                        <ImageView
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:layout_width="wrap_content"
                            android:background="#D0060F" />
    
                        <ImageView
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:layout_width="wrap_content"
                            android:background="#1F0F0F" />
    
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_weight="1"
                            android:layout_height="match_parent"
                            android:background="#FF0F0F" />
                    </LinearLayout>
    
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1"
                        android:orientation="horizontal"
                        android:weightSum="3">
    
                        <ImageView
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:layout_width="wrap_content"
                            android:background="#1F0F0F" />
    
                        <ImageView
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:layout_width="wrap_content"
                            android:background="#D0060F" />
    
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_weight="1"
                            android:layout_height="match_parent"
                            android:background="#FF0F0F" />
                    </LinearLayout>
    
                </LinearLayout>
    
    
    
            </LinearLayout>
        </ScrollView>
      </android.support.v4.widget.NestedScrollView>
    </LinearLayout>
    

    希望对您有所帮助!

    【讨论】:

      【解决方案3】:

      这是您需要做的:首先,我们从主垂直LinearLayout 中删除android:weightSum 属性,并从每个水平LinearLayout 中删除android:layout_weight。我们还需要将id 设置为我们的垂直LinearLayoutandroid:id=@+id/mainContainerLL。这是我从您的布局中导出用于测试的示例布局:

      <android.support.v4.widget.NestedScrollView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:fillViewport="true">
      
          <ScrollView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_below="@+id/toolbar"
              android:fillViewport="true">
      
              <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical">
      
                  <LinearLayout
                      android:id="@+id/rowContainerLL"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:orientation="vertical"
                      android:padding="1dp">
      
      
                      <LinearLayout
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:background="@android:color/holo_red_light"
                          android:orientation="horizontal"
                          android:weightSum="3">
      
                      </LinearLayout>
      
                      <LinearLayout
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:background="@android:color/darker_gray"
      
                          android:orientation="horizontal"
                          android:weightSum="3">
      
      
                      </LinearLayout>
      
                      <LinearLayout
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:background="@android:color/holo_red_light"
      
                          android:orientation="horizontal"
                          android:weightSum="3">
                      </LinearLayout>
      
      
                      <LinearLayout
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:background="@android:color/darker_gray"
      
                          android:orientation="horizontal"
                          android:weightSum="3">
      
                      </LinearLayout>
      
                      <LinearLayout
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:background="@android:color/holo_red_light"
      
                          android:orientation="horizontal"
                          android:weightSum="3">
      
                      </LinearLayout>
      
                      <LinearLayout
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:background="@android:color/darker_gray"
      
                          android:orientation="horizontal"
                          android:weightSum="3">
      
                      </LinearLayout>
      
                  </LinearLayout>
      
      
              </LinearLayout>
          </ScrollView>
      </android.support.v4.widget.NestedScrollView>
      

      现在,对于 java 编码部分:我们使用 ViewTreeObserver 在运行时获取高度(观察者在即将绘制视图并计算它们的高度时给我们一个回调,所以我们需要它)。在那里,我们为每个水平的LinearLayout 设置高度:

      final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.rowContainerLL);
          ViewTreeObserver viewTreeObserver = linearLayout.getViewTreeObserver();
      
          viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
              @Override
              public boolean onPreDraw() {
                  linearLayout.getViewTreeObserver().removeOnPreDrawListener(this);
      
                  View child;
                  LinearLayout.LayoutParams params;
                  final int viewHeight = linearLayout.getHeight() / 4;
      
                  for (int i = 0; i < linearLayout.getChildCount(); i++) {
                      child = linearLayout.getChildAt(i);
      
                      params = (LinearLayout.LayoutParams) child.getLayoutParams();
                      params.height = viewHeight;
      
                      child.setLayoutParams(params);
                  }
      
                  return false;
              }
          });
      

      【讨论】:

      • 我用过 getWindowManager().getDefaultDisplay().getMetrics();并且除以 4 到 metric.height 给我 height 。它运作良好并解决了我的问题。你能告诉我我的代码和你的代码有什么区别吗?
      • @FaisalAhmed 我也尝试过,但是我想我对状态栏/通知栏的值有点不准确,所以我恢复了这个。此外,每当我使用ViewTreeObserver 并且必须处理跨越整个屏幕的视图容器时,我更喜欢简单地调用getHeight(),因为我确信该值是合法的(由于ViewTreeObserver)并且应该准确返回可用于绘制的空间。但是,我想我们使用什么方法应该不会有太大的不同。
      • 所以我希望我以正确的方式做到了。你的建议真的很有帮助,非常感谢
      猜你喜欢
      • 1970-01-01
      • 2021-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-05
      相关资源
      最近更新 更多