【问题标题】:GridView elements horizontal spacing not working at allGridView 元素水平间距根本不起作用
【发布时间】:2014-02-25 08:38:52
【问题描述】:

这就是我的网格视图的样子: a http://imageshack.com/a/img41/7857/roir.png 我使用一些在线 JSON 解析并获取图像缩略图链接,然后将其作为数组传递给自定义适配器并设置图像,我再次使用一些第 3 方库等。但第 3 方库仅用于设置图像到图像视图。就是这样。

所以我有一个主要的 FragmentActivity。它里面有2个碎片。您看到的文本框在一个片段(搜索片段)内,在它上面,gridview 由第二个片段保存。 (结果片段)

结果片段的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f0f0f0"
    tools:context="com.example.wikipicsearch.MainActivity" >

    <GridView
        android:id="@+id/gridView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:columnWidth="100dp"
        android:gravity="center"
        android:numColumns="auto_fit"
        android:verticalSpacing="5dp"
        android:drawSelectorOnTop="true"
        android:stretchMode="columnWidth" >
    </GridView>

</RelativeLayout>

我传递给自定义适配器的布局代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dp" 
    android:clickable="true"
    android:background="@drawable/grid_color_selector"
    android:focusable="true">

    <ImageView
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="0dp"
        android:layout_gravity="center" >
    </ImageView>
</LinearLayout>

现在,我不知道为什么每个线性布局之间没有水平间距。我尝试使用android:layout_marginLeft 等等等等......根本没有任何效果!我让网格视图的片段看起来是灰色的,这样网格中的每个元素都会留下一些间距,看起来应该不错。这个你能帮我吗。蒂亚!

【问题讨论】:

  • 您是否尝试在 GridView 的 xml 中设置 android:horizo​​ntalSpacing="5dp"?
  • 试试这个..,添加水平间距.... android:horizo​​ntalSpacing="10dp"
  • 还为您的 LinearLayout 添加背景颜色,这样当您进入此屏幕时,它会显示项目覆盖了多少空间,然后您可以相应地添加/删除边距和填充。
  • 你在开玩笑吧!!!天啊!我不知道为什么当我尝试之前它没有给我一个选项来做那个(ctrl +空格自动完成帮助)....哦,伙计..我浪费了一天!非常感谢,对帽子感到抱歉,我完全……你知道吗。 :(

标签: android android-layout gridview imageview android-ui


【解决方案1】:

Android:horizontalSpacing="20dp" 这不起作用,如果您在为网格设置的项目布局的根元素(ex-card)中有固定大小

像这样设置宽度 - android:layout_width="match_parent"

活动布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".views.legacy_grid_view.LegacyGridActivity">


    <GridView
        android:layout_height="match_parent"
        android:id="@+id/legacyGrid"
        android:background="@color/teal_700"
        android:layout_width="match_parent"
        android:numColumns="2"
        android:horizontalSpacing="20dp"
        android:verticalSpacing="20dp"/>
</LinearLayout>

项目布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="200dp"
    app:cardCornerRadius="10dp"
    app:cardElevation="5dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginStart="32dp"
            android:layout_marginTop="32dp"
            android:layout_marginEnd="32dp"
            android:layout_marginBottom="60dp"
            android:scaleType="fitCenter"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@android:drawable/presence_away" />

        <TextView
            android:id="@+id/legacyGridEmail"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginTop="16dp"
            android:layout_marginBottom="16dp"
            android:text="Email"
            android:textSize="20sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView2" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

结果

【讨论】:

    猜你喜欢
    • 2017-10-07
    • 1970-01-01
    • 2021-12-26
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多