【问题标题】:How to make the content of a grid layout stay inside the screen如何使网格布局的内容留在屏幕内
【发布时间】:2017-03-19 09:31:19
【问题描述】:

我正在开发一个应用程序,它有 100 个按钮。我想把所有的按钮都放在屏幕上。我正在使用带有所有按钮的 GridLayout,我遇到的问题是按钮正在离开屏幕。

这是代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/content_main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main"
    android:layout_column="1">

 <GridLayout
    android:id="@+id/GridLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnCount="6"
    android:rowCount="17"
    android:orientation="vertical">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:srcCompat="@drawable/numero_1"
        android:id="@+id/button1"/>

我的问题是:这可能吗?这是最好的方法吗?

我进行了其他测试,更改了图像的大小。结果不是我预期的标题

不同尺寸图片的结果

正如您在第二张图片中看到的那样,行中的按钮位于屏幕内部,但按钮的位置不对,行的最后一个按钮和屏幕末尾之间有很多空间。

我正在寻找的解决方案是有一个不允许按钮离开屏幕的布局,并且如果屏幕大小不同,它会自动调整按钮和边缘之间的空间大小布局会自动适应屏幕大小。

【问题讨论】:

    标签: android autoresize android-gridlayout android-imagebutton


    【解决方案1】:

    我认为这是不可能的。我建议您在一个水平方向上使用 6 个垂直线性布局,内部的权重为 1。我知道这不是最优化的方式,但它是最简单的。另一个是保留 GridLayout 并计算图像的大小并从 Java 代码设置它们的大小。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-20
      • 2013-01-05
      • 2022-01-18
      • 2019-07-18
      • 2021-11-24
      • 1970-01-01
      • 2017-09-28
      • 2019-10-30
      相关资源
      最近更新 更多