【问题标题】:How to data view like gridview with linearlayout如何使用线性布局像gridview一样数据视图
【发布时间】:2016-11-26 16:07:05
【问题描述】:

我写了一个代码,其中, 一个 Layout 是用 weightsum 划分的。 每个权重都包含一个线性布局。

我有一个字符串数组列表。 我必须把数据放在......

> 1.         2.          3.
> 4.          5.

提供一些代码或示例。

【问题讨论】:

  • 我不明白这个问题到底是什么......如果你想要一个网格,请使用带有适配器的 RecyclerView。适配器确实可以与 ArrayLists 一起使用,所以如果这是您需要的,我会在答案中添加一些示例代码。特别是,LinearLayout 用于线性布局,而不是网格。
  • 你能帮我,禁用gridview滚动视图吗?
  • 如何使用线性布局替代gridview?
  • 我的回答有帮助您解决问题吗?
  • 记得接受你的问题的答案...如果他们有帮助

标签: android android-layout listview android-studio android-intent


【解决方案1】:

http://www.vogella.com/tutorials/AndroidRecyclerView/article.html#using-lists-or-grids-in-android

这显示了如何使用由字符串的 ArrayList 组成的适配器创建 RecyclerView。

特别是,这是您需要的代码:

    mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

    mLayoutManager = new GridLayoutManager(this, numberOfColumns);
    mRecyclerView.setLayoutManager(mLayoutManager);

    // specify an adapter (see also next example)
    mAdapter = new MyAdapter(myDataset); //Where myDataset is your ArrayList<String> and MyAdapter is described in depth in the link I provided
    mRecyclerView.setAdapter(mAdapter);

既然你还在问LinearLayout,我会试着把它弄清楚。

您可以使用 LinearLayout 创建单行或单列等宽视图,方法是使用“layout-width”属性或以编程方式使用以下内容:

LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
p.weight = 1;

view.setLayoutParams(p);

此外,LinearLayout 不适合动态数据列表,即使理论上您可以遍历列表并使用“addView”手动创建所有内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-17
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    相关资源
    最近更新 更多