【问题标题】:Layout weight in GridviewGridview 中的布局权重
【发布时间】:2016-06-27 03:13:02
【问题描述】:

当涉及到各种屏幕尺寸时,我希望卡片填满整个屏幕。我想使用权重或任何其他可以做到这一点的选项。

        <!--Card 1-->
        <android.support.v7.widget.CardView
            android:id="@+id/cardView1"
            android:layout_height="200sp"
            android:layout_width="150sp">

        </android.support.v7.widget.CardView>

        <!--Card 2-->
        <android.support.v7.widget.CardView
            android:id="@+id/cardView2"
            android:layout_height="200sp"
            android:layout_width="150sp">

        </android.support.v7.widget.CardView>

        <!--Card 3-->
        <android.support.v7.widget.CardView
            android:id="@+id/cardView3"
            android:layout_height="200sp"
            android:layout_width="150sp">

        </android.support.v7.widget.CardView>

        <!--Card 4-->
        <android.support.v7.widget.CardView
            android:id="@+id/cardView4"
            android:layout_height="200sp"
            android:layout_width="150sp">

        </android.support.v7.widget.CardView>
    </GridLayout>

【问题讨论】:

    标签: android


    【解决方案1】:

    试试这个

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="2">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="2">
    
        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    
        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="2">
    
        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    
        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    
    </LinearLayout>
    

    这可能会在非常旧的设备上导致一些性能问题,但在较新的设备上应该不会出现问题。 此外,如果需要,只需在卡片周围添加一些边距以显示阴影 希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      为此,您必须动态设置网格视图的高度。 在您的 Activity 类中定义。

           public static int heighth;
           public static int width;
           DisplayMetrics metrics = new DisplayMetrics();
              getWindowManager().getDefaultDisplay().getMetrics(metrics);
      
              widthh = metrics.widthPixels;
              heighth = metrics.heightPixels;
      

      然后在你的 gridview 适配器类中写入

      gridView.setMinimumHeight((Home.heighth)/3-70);
      

      Here Home 是您的活动课程Home.java。我除以 3 因为我只有 3 行,而减去 70 只是为了调整视图。希望对您有所帮助!

      【讨论】:

      • 这不适用于带有屏幕菜单按钮的设备,例如 Galaxy S9 等。显示尺寸将返回整个屏幕的尺寸。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-26
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-11
      相关资源
      最近更新 更多