【问题标题】:buttons don't show up or remove each other in GridLayout按钮在 GridLayout 中不显示或相互删除
【发布时间】:2016-09-14 00:31:08
【问题描述】:

这是我的问题:当一些图像被放置在 GridLayout 中时,它们周围的其他应用图像消失了。我注意到似乎“推出”其他图像的图像从视图有非常大的图像

【问题讨论】:

  • 你能发布你的布局和填充布局的代码吗?我不认为有关适配器的错误消息表明存在问题。
  • @Cheticamp 我更新了 Layout Xml 文件。如果你看到任何东西,请告诉我。非常感谢您提前
  • XML 是全部在一个文件中,还是您将几个不同的布局附加在一起?我没有看到您的 GridLayout 的 XML。
  • @Cheticamp XML 只是一个文件,我在相应的 java 文件中定义了我的网格布局 ^^^ 上面。刚刚编辑:)
  • 你的 XML 没有意义。您似乎将按钮布局与容器布局混合在一起 - 两者应该是分开的。您能否将代码发布到您实际创建每个按钮的位置并将其添加到适配器中。

标签: java android image android-layout layout


【解决方案1】:

我将概述 XML 中可能给您带来一些问题的一些区域。首先,您需要一个 XML 布局文件 看起来像下面的内容,它将保存您的顶级布局。这将在它自己的文件中。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/home_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

请注意,RecyclerView 在 LinearLayout 中是独立的。接下来您将需要一个单独的 XML 文件 这将为布局的每个单元格保存 ImageButton 和 TextView 的布局。这将适合每个 RecyclerView 中网格的单元格。我使用android:scaleType="centerCrop" 将大图像放入 ImageButton。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp">

    <ImageButton
        android:id="@+id/new_app_button"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_centerHorizontal="true"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop" />

    <TextView
        android:id="@+id/new_app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/new_app_button"
        android:layout_centerInParent="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp" />

</RelativeLayout>

onCreateViewHolder() of RecyclerViewHolder 将如下所示,假设名称为“grid_cell.xml” 上面的 XML 文件。

@Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
    View layoutView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.grid_cell, parent, false);
    RecyclerViewHolders holder = new RecyclerViewHolders(layoutView);
    return holder;
}

试试这个。我认为其他一切都很好。希望对你有帮助。

【讨论】:

  • 非常感谢它解决了这个问题!我现在就批准你的问题 :) - Emily
  • 这个问题不是有50赏金吗?
  • 是的,50 分从我和我读到的内容中被删除了,因为我给你的答案是正确的,给你一个绿色标记,你应该把 50 分转移给你......除非它等待 7 天,不完全确定它是如何工作的
  • 我不认为赏金会自动授予接受的答案,直到赏金期结束加上宽限期。我之所以这么问,是因为您的问题中所有提到的赏金都消失了,我觉得这很奇怪。
  • 嗯,没错,赏金没有出现。我可以给管理员发消息。并询问发生了什么,以便您获得赏金。它被我收回了,所以是的,我希望你拥有它:)
猜你喜欢
  • 2020-04-01
  • 2017-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-27
  • 1970-01-01
相关资源
最近更新 更多