GridLayouts 不变得可滚动

huangapple go评论59阅读模式
英文:

GridLayouts not becoming Scrollable

问题

以下是您提供的XML代码的翻译部分:

我试图使GridLayout可滚动,但我在这方面失败了。问题在于它自动调整到视图内部。

我的XML代码:




<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="134dp"
android:id="@+id/logo_dash"
android:layout_height="100dp"
android:src="@drawable/logo" />



<androidx.cardview.widget.CardView
android:padding="12dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_height="0dp"
android:layout_width="0dp"
app:cardCornerRadius="20sp"
app:cardElevation="32dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>




这是您提供的XML代码,其中包含一个ScrollView,然后是LinearLayout作为子元素,然后在LinearLayout中包含了RelativeLayout和GridLayout两个子元素。

您想要使GridLayout可滚动,但遇到了问题。如果您有关于如何解决这个问题的具体问题,请随时提出。

<details>
<summary>英文:</summary>

I am trying to make the Gridlayout Scrollable but I am failed in doing so. The problem is that it automatically adjusting it inside the view.

My XML Code

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity"
android:fillViewport="true"
android:background="@drawable/bg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

    &lt;de.hdodenhof.circleimageview.CircleImageView
        android:layout_width=&quot;134dp&quot;
        android:id=&quot;@+id/logo_dash&quot;
        android:layout_height=&quot;100dp&quot;
        android:src=&quot;@drawable/logo&quot; /&gt;

    &lt;TextView
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;Home&quot;
        android:layout_toRightOf=&quot;@id/logo_dash&quot;

        android:layout_margin=&quot;12dp&quot;
        android:textSize=&quot;28sp&quot;
        android:textStyle=&quot;bold&quot;
        android:textColor=&quot;@color/black&quot;/&gt;


&lt;/RelativeLayout&gt;
&lt;GridLayout

    android:layout_margin=&quot;6dp&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:rowCount=&quot;3&quot;
    android:columnCount=&quot;1&quot;

    android:columnOrderPreserved=&quot;false&quot;
    android:alignmentMode=&quot;alignMargins&quot;&gt;

    &lt;androidx.cardview.widget.CardView
        android:padding=&quot;12dp&quot;
        android:layout_columnWeight=&quot;1&quot;
        android:layout_rowWeight=&quot;1&quot;
        android:layout_height=&quot;0dp&quot;
        android:layout_width=&quot;0dp&quot;
        app:cardCornerRadius=&quot;20sp&quot;
        app:cardElevation=&quot;32dp&quot;&gt;
        &lt;androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot;
            &gt;

            &lt;ImageView
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;match_parent&quot;
                android:src=&quot;@drawable/g_img&quot;/&gt;

        &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
    &lt;/androidx.cardview.widget.CardView&gt;
    &lt;androidx.cardview.widget.CardView
        android:layout_columnWeight=&quot;1&quot;
        android:layout_rowWeight=&quot;1&quot;
        android:layout_height=&quot;0dp&quot;
        android:layout_width=&quot;0dp&quot;
        app:cardCornerRadius=&quot;20sp&quot;
        app:cardElevation=&quot;32dp&quot;&gt;
        &lt;androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot;
            &gt;

            &lt;ImageView
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;match_parent&quot;
                android:src=&quot;@drawable/g_img&quot;/&gt;

        &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
    &lt;/androidx.cardview.widget.CardView&gt;
    &lt;androidx.cardview.widget.CardView
        android:layout_columnWeight=&quot;1&quot;
        android:layout_rowWeight=&quot;1&quot;
        android:layout_height=&quot;0dp&quot;
        android:layout_width=&quot;0dp&quot;
        app:cardCornerRadius=&quot;20sp&quot;
        app:cardElevation=&quot;32dp&quot;&gt;
        &lt;androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot;
            &gt;

            &lt;ImageView
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;match_parent&quot;
                android:src=&quot;@drawable/g_img&quot;/&gt;

        &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
    &lt;/androidx.cardview.widget.CardView&gt;
    &lt;androidx.cardview.widget.CardView
        android:layout_columnWeight=&quot;1&quot;
        android:layout_rowWeight=&quot;1&quot;
        android:layout_height=&quot;0dp&quot;
        android:layout_width=&quot;0dp&quot;
        app:cardCornerRadius=&quot;20sp&quot;
        app:cardElevation=&quot;32dp&quot;&gt;
        &lt;androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot;
            &gt;

            &lt;ImageView
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;match_parent&quot;
                android:src=&quot;@drawable/g_img&quot;/&gt;

        &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
    &lt;/androidx.cardview.widget.CardView&gt;
    &lt;androidx.cardview.widget.CardView
        android:layout_columnWeight=&quot;1&quot;
        android:layout_rowWeight=&quot;1&quot;
        android:layout_height=&quot;0dp&quot;
        android:layout_width=&quot;0dp&quot;
        app:cardCornerRadius=&quot;20sp&quot;
        app:cardElevation=&quot;32dp&quot;&gt;
        &lt;androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot;
            &gt;
            &lt;ImageView
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;match_parent&quot;
                android:src=&quot;@drawable/g_img&quot;/&gt;

        &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
    &lt;/androidx.cardview.widget.CardView&gt;
&lt;/GridLayout&gt;

</LinearLayout>

</ScrollView>

This is my xml code in which i used scrollview and then linear layout as child then two childs inside linear layout Relative Layout and Grid Layout. 





[![I want this to make scrollable][1]][1]


  [1]: https://i.stack.imgur.com/CGQAd.png

</details>


# 答案1
**得分**: 1

以下是翻译好的部分:

1. 使用 **GridLayout** 的**硬编码**尺寸,比如设置高度为 400dp 或其他任何值。这样它将具有该尺寸,并在该高度范围内滚动。

2. 使用 **NestedScrollView** 代替 **ScrollView**。*ScrollView 不能正确处理也可滚动的视图*,在这里 GridLayout 已经是可滚动的,但当将它放入 ScrollView 中时,它无法正常工作。

以下是一个带有 GridLayout 的 NestedScrollView 示例(作者 - [Mersad Esalati][1] ):

```xml
<android.support.v4.widget.NestedScrollView 
    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">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:columnCount="2"
            android:rowCount="4">
    
            <!-- 填充您的网格 -->
    
        </GridLayout>
    
    </LinearLayout>

</android.support.v4.widget.NestedScrollView>
英文:

There are two ways you can achieve this.

  1. Use hardcoded size for GridLayout. Like set height for ex. 400dp or anything. That way it'll have that size and scroll inside that area of height.

  2. Use NestedScrollView instead of ScrollView. ScrollView doesn't work properly with views that are also scrollable, here Gridlayout is already scrollable, but when you put it inside scrollview it stopped working properly.

Here is an example of NestedScrollView with GridLayout (Credit - Mersad Esalati )

   &lt;android.support.v4.widget.NestedScrollView 
        xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
        xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;&gt;
    
        &lt;LinearLayout
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:orientation=&quot;vertical&quot;&gt;
    
            &lt;GridLayout
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:columnCount=&quot;2&quot;
                android:rowCount=&quot;4&quot;&gt;
    
                &lt;!-- fill your grid --&gt;
    
            &lt;/GridLayout&gt;
    
        &lt;/LinearLayout&gt;
    
    &lt;/android.support.v4.widget.NestedScrollView&gt;

huangapple
  • 本文由 发表于 2023年3月9日 17:02:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682390.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定