我目前正在开发一个使用卡片的应用程序。我在谷歌官方应用程序中看到了卡片的使用。
然而,谷歌的应用程序在手机和平板电脑上略有不同。
在手机上,它显示的是彼此之间的卡,旁边没有太多空间。(它在我的手机上也是这样,在景观模式下)
在平板电脑上,它把两张卡片放在一起。(有时一大和两小,有时两等号)
这里有人能解释一下他们是如何做到的吗?我需要添加什么样的XML或Java,才能将类似的功能添加到我的应用程序中。(目前我的应用程序中有工作卡,但它们的宽度设置为fill_parent,因此它们填充了整个屏幕)
(事先非常感谢:)
发布于 2016-06-13 21:13:37
他们使用RecyclerView和不同的LayoutManager来实现这一点。
代码的示例如下:
// during onCreate
RecyclerView rv = (RecyclerView)findViewById(R.id.recycler);
LayoutManager lm;
if(isPhone) {
if(isLandscape) {
lm = new GridLayoutManager(this, 2);
} else {
lm = new LinearLayoutManager(this);
}
} else {
if(isLandscape) {
lm = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
} else {
lm = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
}
}
rv.setLayoutManager(lm);
rv.setAdapter(... your adapter with the cards...);若要在项目之间实现不同的间隔,可以使用RecyclerView.ItemDecoration。如果您需要头、页脚或其他实用程序,我建议您查看我的库RecyclerViewTools on GitHub。
发布于 2016-06-13 21:12:29
它们为像res/layout-large/my_layout.xml这样的目录中的大型设备定义一个单独的布局文件。Android将自动为设备选择合适的布局。有关更多信息,请查看以下链接:
support.html https://developer.android.com/training/multiscreen/screensizes.html
发布于 2016-06-13 21:13:15
https://stackoverflow.com/questions/37799039
复制相似问题