【问题标题】:How to center GridView on the screen如何在屏幕上居中显示 GridView
【发布时间】:2018-08-07 15:56:16
【问题描述】:

如何使 GridView 在屏幕上居中。我有 GridView 有 4 个项目,每行 2 个项目。我尝试使用相对布局中的填充将 GridView 居中在相对布局内。但是我为多个屏幕开发了应用程序。所以我认为硬编码填充不是一个好习惯。如何让这个 Grid 视图在多个屏幕上居中?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/gridrelativelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="60dp"
    android:paddingBottom="60dp"
    android:paddingLeft="150dp"
    android:paddingRight="150dp"
    tools:context=".MainMenuActivity" >

    <GridView
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#F9F9F9"
        android:gravity="center"
        android:horizontalSpacing="50dp"
        android:numColumns="2"
        android:stretchMode="columnWidth"
        android:verticalSpacing="50dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>

更新代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/gridrelativelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="150dp"
    android:paddingRight="150dp"
    tools:context=".MainMenuActivity" >

    <GridView
        android:id="@+id/gridview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:columnWidth="20dp"
        android:background="#FF0000"
        android:gravity="center"
        android:horizontalSpacing="50dp"
        android:numColumns="2"
        android:verticalSpacing="50dp"
        android:layout_centerInParent = "true" 
       />

</RelativeLayout>

此更新后的代码正确地将 gridView 在屏幕上居中。但是请看,我在相对布局中硬编码了左侧广告右侧的填充。由于我想支持多个屏幕,我想使用以下代码将此填充设置为动态。但它不起作用。

RelativeLayout gridrelativelayout = (RelativeLayout)findViewById(R.id.gridrelativelayout);
        int pad=gridrelativelayout.getWidth()/3;
        gridrelativelayout.setPadding(pad, 0, pad, 0);

【问题讨论】:

  • 在 GridView 中尝试 android:layout_width="wrap_content",layout_height 也一样。
  • 我的问题是不同的。我上面的代码工作正常,居中对齐。但我认为这不是一个好习惯。因为我需要支持多屏幕,如何动态设置填充。
  • 抱歉,您可以通过编程方式完成,或者您可以在 dimensions.xml 中定义值并像这样使用它们:android:paddingTop="@dimen/padding_top"。然后为 large、xlarge 等提供不同版本的 dimensions.xml。
  • 另外我相信你根本不需要RelativeLayout,将GridView设置为你的顶部布局并在那里设置填充。

标签: android


【解决方案1】:

申请RelativeLayout

 android:layout_width="match_parent"
 android:layout_height="match_parent"

对于GridView

android:layout_centerInParent = "true"

【讨论】:

  • 是的,它垂直适合。但是要水平居中我想缩小gridview的宽度,否则gridview会占据整个屏幕的宽度
  • 也使 Gridview android:layout_width="match_parent" android:layout_height="match_parent"
  • no 如果将 match_parent 添加到 GriView,gridview 匹配到父级(Relativelayout)。所以它也不会在垂直的屏幕上居中。
【解决方案2】:

将android:layout_centerInParent = "true" 用于GridView。这肯定有效...

【讨论】:

  • 使 android:layout_centerInParent = "true" 和 gridView layout_height 和 layout_width = wrap_content。它使网格视图仅在 Verticallu 上居中,而不是水平居中。我想根据屏幕缩小网格视图的宽度
【解决方案3】:

如果想将最好的应用程序写入多个屏幕设备,那么您需要阅读以下内容:

Supporting Multiple Screens

它对你很有用:

Supporting Different Screens

【讨论】:

  • 支持多屏的通用文档。这个问题只针对GrdView
【解决方案4】:

使用

android:layout_centerHorizontal = "true"

对于 GridView。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-03
    • 2016-02-17
    • 2014-03-24
    • 2016-01-10
    • 1970-01-01
    • 2021-12-22
    • 2020-01-04
    • 2015-07-11
    相关资源
    最近更新 更多