我希望有一个带有按钮的2x2网格.这只是ICS所以我试图使用给定的新GridLayout.
这是我的布局的XML:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/favorites_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00"
android:rowCount="2"
android:columnCount="2">
<Button
android:text="Cell 0"
android:layout_row="0"
android:layout_column="0"
android:textSize="14dip" />
<Button
android:text="Cell 1"
android:layout_row="0"
android:layout_column="1"
android:textSize="14dip" />
<Button
android:text="Cell 2"
android:layout_row="1"
android:layout_column="0"
android:textSize="14dip" />
<Button
android:text="Cell 3"
android:layout_row="1"
android:layout_column="1"
android:textSize="14dip" />
</GridLayout>
Run Code Online (Sandbox Code Playgroud)
问题是我的视图不能均匀地拉伸到每一行.这会在GridLayout的右侧产生大量额外空间.
我试过设置,layout_gravity="fill_horizontal"
但只适用于行的最后一个视图.这意味着Cell 1一直延伸为Cell 0提供足够的空间.
关于如何解决这个问题的想法?
在Android开发者博客文章介绍GridLayout
节目的跨度如何影响自动索引分配这个图:
我试图用a实际实现它GridLayout
.这是我到目前为止:
<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.commonsware.android.gridlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:orientation="horizontal"
app:columnCount="8">
<Button
app:layout_columnSpan="2"
app:layout_rowSpan="2"
android:layout_gravity="fill_horizontal"
android:text="@string/string_1"/>
<Button
app:layout_columnSpan="2"
android:layout_gravity="fill_horizontal"
android:text="@string/string_2"/>
<Button
app:layout_rowSpan="4"
android:text="@string/string_3"/>
<Button
app:layout_columnSpan="3"
app:layout_rowSpan="2"
android:layout_gravity="fill_horizontal"
android:text="@string/string_4"/>
<Button
app:layout_columnSpan="3"
android:layout_gravity="fill_horizontal"
android:text="@string/string_5"/>
<Button
app:layout_columnSpan="2"
android:layout_gravity="fill_horizontal"
android:text="@string/string_6"/>
<android.support.v7.widget.Space
app:layout_column="0"
android:layout_width="36dp"
/>
<android.support.v7.widget.Space
android:layout_width="36dp"
/>
<android.support.v7.widget.Space
android:layout_width="36dp"
/>
<android.support.v7.widget.Space
android:layout_width="36dp"
/>
<android.support.v7.widget.Space
android:layout_width="36dp"
/>
<android.support.v7.widget.Space
android:layout_width="36dp"
/>
<android.support.v7.widget.Space
android:layout_width="36dp"
/>
<android.support.v7.widget.Space
android:layout_width="36dp"
/>
</android.support.v7.widget.GridLayout>
Run Code Online (Sandbox Code Playgroud)
我必须引入<Space>
元素以确保每列具有最小宽度,否则,我会有一堆零宽度列.
然而,即使有他们,我得到这个:
值得注意的是:
尽管如此android:layout_gravity="fill_horizontal"
,我的带有列跨度的小部件并没有填充跨越的列
尽管有这些android:layout_rowSpan
值,但没有任何东西跨越行
任何人都可以使用博客文章从博客文章中复制图表GridLayout
吗?
谢谢!
使用新的GridLayoutManager:https://developer.android.com/reference/android/support/v7/widget/GridLayoutManager.html
它需要一个明确的跨度计数,所以问题现在变成:你怎么知道每行有多少"跨度"?毕竟这是一个网格.根据测量的宽度,RecyclerView可以容纳多个跨距.
使用旧的GridView
,您只需设置"columnWidth"属性,它将自动检测适合的列数.这基本上是我想要为RecyclerView复制的:
RecyclerView
这似乎是很常见的行为,所以有一种我没有看到的更简单的方法吗?
android android-gridlayout gridlayoutmanager android-recyclerview
在我的片段中,我按以下方式设置GridLayout:
mRecycler.setLayoutManager(new GridLayoutManager(rootView.getContext(), 2));
所以,我只是想改变2
的4
,当用户旋转手机/平板电脑.我已经读过了onConfigurationChanged
,我试图让它适用于我的情况,但它没有以正确的方式进行.当我旋转手机时,应用程序崩溃了......
你能告诉我如何解决这个问题吗?
以下是我找到解决方案的方法,该方法无法正常工作:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int orientation = newConfig.orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
mRecycler.setLayoutManager(new GridLayoutManager(mContext, 2));
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
mRecycler.setLayoutManager(new GridLayoutManager(mContext, 4));
}
}
Run Code Online (Sandbox Code Playgroud)
提前致谢!
android android-orientation android-gridlayout android-recyclerview
我有一个带有GridLayoutManager的RecyclerView,可以显示卡片视图.我希望卡片根据屏幕尺寸重新排列(Google Play应用程序使用其应用程序卡执行此类操作).这是一个例子:
以下是我的应用程序当前的样子:
正如您所看到的那样,这些卡片只是拉伸而不适合从方向更改所形成的空白区域.那我该怎么做呢?
码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Json;
using System.Threading;
using System.Threading.Tasks;
using Android.Media;
using Android.App;
using Android.Support.V4.App;
using Android.Support.V4.Content.Res;
using Android.Support.V4.Widget;
using Android.Support.V7.Widget;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Android.Net;
using Android.Views.Animations;
using Android.Graphics;
using Android.Graphics.Drawables;
using Newtonsoft.Json;
using *******.Adapters;
using *******.Models;
namespace *******.Fragments {
public class Dashboard : GridLayoutBase {
private ISharedPreferences pref;
private SessionManager session;
private string cookie;
private DeviceModel deviceModel;
private RecyclerView …
Run Code Online (Sandbox Code Playgroud) layout android xamarin android-gridlayout android-recyclerview
我的帖子里有两列GridLayout
.我想要做的是让这些列占据屏幕宽度的一半,然后让它的子内容填充它们自己的单元格宽度/高度.我尝试设置子项,fill_parent
但这只会导致第一个接管整个布局.似乎GridLayout不支持weight
?也许有更好的布局可供使用,但我想要一个网格样式布局,这似乎是自然的选择.
我的问题非常类似于如何获得布局,其中一个文本可以增长和椭圆化,但不会吞噬布局上的其他元素,但请阅读下面为什么我不能使用那里提出的TableLayouts.
我正在尝试创建一个基本上如下所示的listview行:
| TextView | 查看1 | 查看2 |
所有视图都包含可变宽度元素.TextView具有ellipsize ="end"设置.视图1应该对齐TextView的左侧,而视图2应该对齐到屏幕的右侧.因此,通常,View 1和View 2之间会有空格.随着TextView中的文本变长,TextView应该增长,将View 1推向右侧,直到没有剩下的空白.然后,ellipsize应该启动,在TextView中剪切文本并在末尾添加省略号("...").
所以,结果看起来应该是这样的:
+----------------------------------------+
| short text [view1] [view2] |
+----------------------------------------+
| long text with ell ... [view1] [view2] |
+----------------------------------------+
Run Code Online (Sandbox Code Playgroud)
我试过了:
这是我试过的GridLayout:
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_gravity="left|fill_horizontal"
android:ellipsize="end"
android:singleLine="true"
android:text="Long text to demonstrate problem with TextView in GridLayout taking up too much space despite ellipsis" />
<TextView
android:layout_gravity="left"
android:text="(view1)" />
<TextView
android:layout_gravity="right"
android:text="(view2)" />
</GridLayout>
Run Code Online (Sandbox Code Playgroud)
视图1和视图2不是真正的TextViews,我只是在示例中使用它们来简化操作.
有没有办法在不使用TableLayouts的情况下实现这一目标?
编辑: 根据要求,这是我尝试用RelativeLayout解决这个问题.在这种情况下,TextView占据了屏幕的整个宽度,因此view1和view2都不可见.
<RelativeLayout
android:layout_width="match_parent" …
Run Code Online (Sandbox Code Playgroud) 我有一个带有gridlayout的recyclerview.我想要的是当用户滚动到列表的末尾时(请参阅我的坏模型),应该有一个高度为50dp的空白空间,这与我的网格尺寸不同.
请注意,此空间仅在最末端可见,因为我不想更改布局.我可以这样做,以便recycerview的边距为50dp,但我不想这样做.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:scrollbars="vertical"
/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud) android android-layout android-gridlayout android-recyclerview
GridView是一个扩展 AdapterView的类,这意味着它可以有效地显示网格样式的单元格,并在用户滚动时回收旧视图以显示为新视图.
有时,您希望获得一个特殊的UI,它类似于Windows Phone Tiles UI,具有不同大小的单元格.
像这样的东西:
AABB
AACC
AADD
AADD
Run Code Online (Sandbox Code Playgroud)
每个字母代表其细胞的一部分,因此细胞A是2x4,细胞B和细胞C各占2x1,细胞D是2x2.
它可能甚至更多,其中单元格D比我所示的稍微高一点,并且在它下方有另一个单元格,因此D的末端和A的末尾不必对齐.
具有这种风格的应用程序的一个例子是pinterest.
GridView不允许这样的事情.
事实上,我尝试的每个解决方案都有问题.想问一下是否有其他替代品或更好的解决方案.
有多种方法可以解决此问题:
作为扩展AdapterView 的类,GridView可以为每个项目设置一个类型(使用BaseAdapter),这将允许您设置如何布局单元格.
但是,它仍会限制你,因为你会得到一排物品,一个在另一个之下.你必须让它们对齐.
GridLayout是一个相对较新的布局,它非常灵活.谷歌为它发布了一个很好的兼容性库,支持API7及更高版本.
但是,它不使用任何视图的回收,因此如果您希望显示大量项目,这是一个糟糕的选择.
如果您有很多项目,则需要为它们创建所有视图.
QuiltView库 - 从GridLayout扩展,所以基本上有类似的问题.
StaggeredGridView - 看起来最有前途,但有很多错误,特别是在改变方向时.这样的错误包括空单元格,坏滚动和NPE(罕见但仍然发生).我也不确定它是否支持单独使用自定义单元而不是imageView.
其他解决方案,如此处所述.
有谁知道这个问题的另一种替代方案?对于我展示的任何解决方案,可能有一些解决方法吗?
编辑:我认为关于StaggeredGridView,滚动和异常可以通过使用在getView中设置其大小的普通ImageView来解决.我认为它以一种奇怪的方式工作的原因是样本在从互联网加载后更新了imageview的大小.
但是,空单元格问题仍然存在于此库中.不仅如此,即使不改变方向,它们的尺寸也会发生很大变化.
编辑:目前,我认为最好的解决方案是使用 PinterestLikeAdapterView库.
它没有我能找到的任何问题.
但是,它不能使项目占用超过1个单元格宽度.尽管如此,这还是非常好的.
编辑:遗憾的是它与notifyDataSetChanged有问题.我在这里报道了这件事.
android cells android-gridview android-adapterview android-gridlayout
我在android材料设计api工作,想要以网格格式显示一些数据.我试过两个GridLayout
,StaggeredGridlayout
看起来都一样.一般信息,我想问一下Gridlayout
和之间的区别是StaggeredGridlayout
什么?
谢谢.
android android-layout android-gridlayout staggeredgridlayout