网格布局+滚动视图

2024-04-04

我想创建一个像 Android 市场首页一样的布局。 我正在尝试使用网格布局,因为我想放置不同大小和位置的图像视图,我还需要输入滚动视图。

实际上我的问题是如何在 android.support.v7.widget.GridLayout 中输入滚动视图?

我试过了:

<?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/com.astuetz.viewpager.extensions.example"
     android:id="@+id/scrollView1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" >

<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:columnCount="10" >

    <ImageView
        android:id="@+id/ImageView1"
        android:layout_width="130dp"
        android:layout_height="130dp"
        app:layout_column="2"
        app:layout_gravity="left"
        app:layout_row="1"
        android:src="@drawable/tab_background_focused" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_column="5"
        app:layout_gravity="bottom"
        app:layout_row="1"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <ImageView
        android:id="@+id/ImageView2"
        android:layout_width="130dp"
        android:layout_height="130dp"
        app:layout_column="1"
        app:layout_columnSpan="3"
        app:layout_gravity="left"
        app:layout_row="2"
        android:src="@drawable/tab_background_focused" />

    <ImageView
        android:id="@+id/ImageView3"
        android:layout_width="130dp"
        android:layout_height="130dp"
        app:layout_column="2"
        app:layout_gravity="left"
        app:layout_row="4"
        app:layout_rowSpan="2"
        android:src="@drawable/tab_background_focused" android:adjustViewBounds="true"/>

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="130dp"
        android:layout_height="130dp"
        app:layout_column="2"
        app:layout_gravity="left"
        app:layout_row="7"
        android:src="@drawable/background_dots" />


    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="130dp"
        android:layout_height="130dp"
        app:layout_column="2"
        app:layout_gravity="left"
        app:layout_row="9" android:adjustViewBounds="true" android:src="@drawable/tab_background"/>


    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="130dp"
        android:layout_height="match_parent"
        app:layout_column="2"
        app:layout_gravity="left"
        app:layout_row="9"
        android:adjustViewBounds="true" />

   <ImageView
        android:id="@+id/imageView6"
        android:layout_width="130dp"
        android:layout_height="130dp"
        app:layout_column="2"
        app:layout_gravity="left"
        app:layout_row="9" android:adjustViewBounds="true"/>

      <ImageView
        android:id="@+id/imageView7"
        android:layout_width="130dp"
        android:layout_height="130dp"
        app:layout_column="2"
        app:layout_gravity="left"
        app:layout_row="2" /> 


    <android.support.v7.widget.Space
        android:id="@+id/space1"
        android:layout_width="145dp"
        android:layout_height="1dp"
        app:layout_column="0"
        app:layout_gravity="left"
        app:layout_row="0" />

    <android.support.v7.widget.Space
        android:layout_width="1dp"
        android:layout_height="21dp"
        app:layout_column="0"
        app:layout_row="0" />

    <android.support.v7.widget.Space
        android:id="@+id/space2"
        android:layout_width="1dp"
        android:layout_height="127dp"
        app:layout_column="0"
        app:layout_gravity="left"
        app:layout_row="1" />

    <android.support.v7.widget.Space
        android:id="@+id/space3"
        android:layout_width="10dp"
        android:layout_height="1dp"
        app:layout_column="0"
        app:layout_gravity="left"
        app:layout_row="0" />

    <android.support.v7.widget.Space
        android:id="@+id/space4"
        android:layout_width="1dp"
        android:layout_height="10dp"
        app:layout_column="0"
        app:layout_gravity="left"
        app:layout_row="1" />

    <android.support.v7.widget.Space
        android:id="@+id/space5"
        android:layout_width="44dp"
        android:layout_height="1dp"
        app:layout_column="3"
        app:layout_gravity="left"
        app:layout_row="0" />

    <android.support.v7.widget.Space
        android:layout_width="1dp"
        android:layout_height="58dp"
        app:layout_column="0"
        app:layout_row="5" />

    <android.support.v7.widget.Space
        android:layout_width="1dp"
        android:layout_height="10dp"
        app:layout_column="0"
        app:layout_row="3" />

    <android.support.v7.widget.Space
        android:id="@+id/space6"
        android:layout_width="1dp"
        android:layout_height="17dp"
        app:layout_column="0"
        app:layout_gravity="left"
        app:layout_row="6" />

    <android.support.v7.widget.Space
        android:id="@+id/space7"
        android:layout_width="238dp"
        android:layout_height="1dp"
        app:layout_column="4"
        app:layout_gravity="left"
        app:layout_row="0" />

    <android.support.v7.widget.Space
        android:id="@+id/space8"
        android:layout_width="1dp"
        android:layout_height="16dp"
        app:layout_column="0"
        app:layout_gravity="left"
        app:layout_row="8" />

    <android.support.v7.widget.Space
        android:layout_width="26dp"
        android:layout_height="1dp"
        app:layout_column="2"
        app:layout_row="0" />

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

在这段代码中,imageview最终开始互相覆盖。


我解决了这个问题,您需要保持线性布局结构,然后在其中添加 gridlayout 并设置layout_height =“the_value_you_need”。像这样:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" xmlns:app="http://schemas.android.com/apk/res/com.astuetz.viewpager.extensions.example">




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



        <android.support.v7.widget.GridLayout
            android:layout_width="match_parent"
            android:layout_height="900dp" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="130dp"
                android:layout_height="130dp"
                app:layout_column="0"
                app:layout_columnSpan="2"
                app:layout_gravity="left"
                app:layout_row="0"
                app:layout_rowSpan="2"
                android:src="@drawable/tab_background_pressed" />

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="130dp"
                android:layout_height="130dp"
                app:layout_column="0"
                app:layout_gravity="right"
                app:layout_row="3"
                android:src="@drawable/tab_background_pressed" />

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="130dp"
                android:layout_height="130dp"
                app:layout_column="0"
                app:layout_gravity="left"
                app:layout_row="5"
                android:src="@drawable/tab_background_pressed" />

            <ImageView
                android:id="@+id/imageView4"
                android:layout_width="130dp"
                android:layout_height="130dp"
                app:layout_column="0"
                app:layout_gravity="left"
                app:layout_row="7"
                android:src="@drawable/tab_background_pressed" />

            <ImageView
                android:id="@+id/imageView5"
                android:layout_width="130dp"
                android:layout_height="130dp"
                app:layout_column="0"
                app:layout_gravity="left"
                app:layout_row="7"
                android:src="@drawable/tab_background_pressed" />

            <android.support.v7.widget.Space
                android:layout_width="32dp"
                android:layout_height="1dp"
                app:layout_column="0"
                app:layout_row="0" />

            <android.support.v7.widget.Space
                android:id="@+id/space1"
                android:layout_width="1dp"
                android:layout_height="16dp"
                app:layout_column="0"
                app:layout_gravity="left"
                app:layout_row="2" />

            <android.support.v7.widget.Space
                android:id="@+id/space2"
                android:layout_width="1dp"
                android:layout_height="16dp"
                app:layout_column="0"
                app:layout_gravity="left"
                app:layout_row="4" />

            <android.support.v7.widget.Space
                android:id="@+id/space3"
                android:layout_width="1dp"
                android:layout_height="16dp"
                app:layout_column="0"
                app:layout_gravity="left"
                app:layout_row="6" />
        </android.support.v7.widget.GridLayout>

    </LinearLayout>


</ScrollView>

要实现水平滚动,只需将layout_widht更改为固定值即可。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

网格布局+滚动视图 的相关文章

  • 如何从 AAR 依赖项中排除特定资源?

    有没有一种相当简单的方法来实现模块的build gradle文件来指示应排除依赖项中的某些文件 我特别感兴趣的是从 AAR 中排除某些资源 金丝雀泄密 https github com square leakcanary是一个有趣的库 可帮
  • onActivityResult 中的完成活动不起作用

    我有一对必须同生共死的活动 基本上 AlphaActivity 会做一些工作 然后发送一个意图 startActivityForResult 对于 Beta 活动 当 Beta 活动完成时 我希望它发送一个意图 startActivity
  • Espresso - 检查使用按钮按下意图打开哪个活动?

    是否可以跟踪按下某个按钮后打开了哪个 Activity 我有一个测试 其中当单击 按下按钮时 it 向服务器发送请求 直到发送请求时 它打开一个活动 验证是否执行成功在测试中 我需要检查打开的 Activity 是什么 我的测试示例 检查
  • 为什么 Kotlin 数据类可以在 Gson 的不可空字段中包含空值?

    在 Kotlin 中你可以创建一个data class data class CountriesResponse val count Int val countries List
  • 如何改变android中menuItem的背景颜色?

    我正在以编程方式将菜单项添加到菜单中 我想在选择特定项目时添加背景颜色 如何为 menuItem 添加背景 您的回答将不胜感激 虽然其他答案提供了更改样式 这会影响all菜单项 据我了解 需要更改一个菜单项 我建议你使用android ac
  • 如何从另一个xml文件动态更新xml文件?

    我想从另一个 xml 文件更新 xml 文件 我使用了一个 xml 文件 如下所示 one xml
  • 更改 AChartEngine 中的图例大小

    我想专门更改饼图的图例大小输出 我已经尝试了所有可以找到的 AChartEngine 方法 但没有一个只能更改图例文本大小 我必须重写 onDraw 函数吗 如果是这样 怎么办 要设置图例高度 请使用 renderer setLegendH
  • 将标题设置为操作栏时出现空指针异常

    Error Caused by java lang NullPointerException Attempt to invoke virtual method void android app ActionBar setTitle java
  • OpenCV InRange 参数

    我在 Android 上使用 OpenCV 来实时查找特定颜色的圆圈 我的第一步是仅保留与我正在寻找的定义颜色相对应的像素 在本例中为红色或绿色 示例图像 https i stack imgur com CIozU jpg 为此 我正在使用
  • 无法将 Tesseract OCR 模块添加到 Android Studio

    我按照此处找到的分步指南进行操作 https www codeproject com Articles 840623 Android Character Recognition https www codeproject com Artic
  • 如何检测 Google Play 上是否有我的应用程序的更新? [复制]

    这个问题在这里已经有答案了 有没有办法以编程方式检查 Google Play 上我的应用程序是否有更新 以便通知用户 我知道 android google play 有自动通知 但我想使用我自己的通知 弹出消息来更新可用性 有点像 Vibe
  • 如何覆盖日期选择器的高度和宽度以填充父布局

    我有一个活动包含一个日期选择器 我想设置DatePicker适合屏幕 我试过这个答案 https stackoverflow com questions 6674667 how to customize date pickers width
  • 如何在android sdk上使用PowerMock

    我想为我的 android 项目编写一些单元测试和仪器测试 然而 我遇到了一个困扰我一段时间的问题 我需要模拟静态方法并伪造返回值来测试项目 经过一些论坛的调查 唯一的方法是使用PowerMock来模拟静态方法 这是我的 gradle 的一
  • 按名称查找视图

    是否可以通过名称而不是 id 来查找视图 findViewById R id someView 但我想做这样的事情 findViewByName someView 在处理 xml 时 您必须通过标识符查找视图 但是您可以使用以下方式查找标识
  • Jetpack Compose 部分或开放侧边框

    我正在尝试绘制部分或一侧开放的矩形圆形边框以实现此效果 玩了一下之后我得到了这个 这是通过以下方式完成的 RoundedCornerShape topStartPercent 50 bottomStartPercent 50 start R
  • OpenGL ES 2.0 屏幕闪烁

    我面临着一个大问题 我正在使用带有 Android 4 0 3 的 Transformer tf101 选项卡 我的应用程序使用自定义 OpenGL ES 2 0 表面 我正在用纹理渲染多个平面 该纹理大约发生变化 每秒 20 次 并通过传
  • 基于BluetoothChat示例通过蓝牙套接字发送文件

    大家好 根据我之前问的一个问题 我已经能够将文件转换为其他字节数组 以便使用以下写入方法 public void sendFile Log d TAG sending data InputStream inputStream null Ur
  • Android应用程序kill事件捕获

    我想在我的应用程序被终止时执行一些操作 可以使用哪种方法来实现此目的 我正在开发 Android 5 0 这个问题的关键在于 您必须了解您的申请是否可以收到任何 当您的应用程序在任何情况下被终止时的额外回调 下面的答案是由德文连线 http
  • 使用支持库中的 BottomSheet 时如何调暗背景?

    怎样才能让背景像显示的那样变暗here https material design storage googleapis com publish material v 8 material ext publish 0Bzhp5Z4wHba3
  • 在 Android 中更新到 API 26 时,清单合并失败并出现多个错误

    我尝试使用 API 26 更新我的 gradle 安卓工作室2 3 3 但我在编译项目时遇到以下错误 这是我收到的错误的屏幕截图 应用级别build gradle Top level build file where you can add

随机推荐