自定义视图的不同单元格大小网格
Different Cell size grid for custom views
我正在做一个项目,我需要实现一个网格来显示 "Widgets"(自定义视图),由最终用户动态重新排序。或多或少 与 Android 启动器及其小部件 相同,用户可以在网格中移动的小部件(与大多数启动器一样,长按)。这是我正在寻找的内容的预览:
Every color represents one different kind of "Widget"
我试过使用 GridLayout 和 TableLayout,但我无法 "merge" 单元格来调整 Widget 的布局。
网格本身是一个 8 x 12 单元格的网格,其中每个 Widget 可以使用超过 1 个单元格,例如,我配置了一个 "Widget button",即 2 x 3 (h | w)。
是否有适合我需要的布局,或者我必须创建自己的布局?
谢谢你,亚伯!
您的应用可以使用带有回收视图的 staggeredGridLayout 吗? http://developer.android.com/reference/android/support/v7/widget/StaggeredGridLayoutManager.html
虽然所有列的大小都必须相同...有点像下面的
取自这个例子https://guides.codepath.com/android/using-the-recyclerview
我使用以下代码以编程方式创建视图并设置它们 PercentRelativeLayout.LayoutParams
:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_percent_relative);
ViewGroup root = (ViewGroup)findViewById(R.id.perecent_layout_view);
View blueWidget = new View(this);
blueWidget.setBackground(blue);
root.addView(blueWidget);
setUpLayout(blueWidget, 2.0f/8.0f, 6.0f/8.0f, 6.0f/8.0f, 0.0f, 2.0f/8.0f, 0.0f);
View redWidget = new View(this);
redWidget.setBackground(red);
root.addView(redWidget);
setUpLayout(redWidget, 5.0f/8.0f, 2.0f/8.0f, 3.0f/8.0f, 0.0f, 0.0f, 6.0f/8.0f);
}
private void setUpLayout(View view, float widthPercent,
float heightPercent,
float leftMarginPercent,
float rightMarginPercent,
float topMarginPercent,
float bottomMarginPercent) {
PercentRelativeLayout.LayoutParams layoutParams = new PercentRelativeLayout.LayoutParams(view.getLayoutParams());
PercentLayoutHelper.PercentLayoutInfo percentLayoutInfo = layoutParams.getPercentLayoutInfo();
percentLayoutInfo.heightPercent = heightPercent;
percentLayoutInfo.widthPercent = widthPercent;
percentLayoutInfo.topMarginPercent= topMarginPercent;
percentLayoutInfo.bottomMarginPercent= bottomMarginPercent;
percentLayoutInfo.leftMarginPercent= leftMarginPercent;
percentLayoutInfo.rightMarginPercent= rightMarginPercent;
view.setLayoutParams(layoutParams);
}
视图如下所示:
我正在做一个项目,我需要实现一个网格来显示 "Widgets"(自定义视图),由最终用户动态重新排序。或多或少 与 Android 启动器及其小部件 相同,用户可以在网格中移动的小部件(与大多数启动器一样,长按)。这是我正在寻找的内容的预览: Every color represents one different kind of "Widget"
我试过使用 GridLayout 和 TableLayout,但我无法 "merge" 单元格来调整 Widget 的布局。
网格本身是一个 8 x 12 单元格的网格,其中每个 Widget 可以使用超过 1 个单元格,例如,我配置了一个 "Widget button",即 2 x 3 (h | w)。
是否有适合我需要的布局,或者我必须创建自己的布局?
谢谢你,亚伯!
您的应用可以使用带有回收视图的 staggeredGridLayout 吗? http://developer.android.com/reference/android/support/v7/widget/StaggeredGridLayoutManager.html
虽然所有列的大小都必须相同...有点像下面的
取自这个例子https://guides.codepath.com/android/using-the-recyclerview
我使用以下代码以编程方式创建视图并设置它们 PercentRelativeLayout.LayoutParams
:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_percent_relative);
ViewGroup root = (ViewGroup)findViewById(R.id.perecent_layout_view);
View blueWidget = new View(this);
blueWidget.setBackground(blue);
root.addView(blueWidget);
setUpLayout(blueWidget, 2.0f/8.0f, 6.0f/8.0f, 6.0f/8.0f, 0.0f, 2.0f/8.0f, 0.0f);
View redWidget = new View(this);
redWidget.setBackground(red);
root.addView(redWidget);
setUpLayout(redWidget, 5.0f/8.0f, 2.0f/8.0f, 3.0f/8.0f, 0.0f, 0.0f, 6.0f/8.0f);
}
private void setUpLayout(View view, float widthPercent,
float heightPercent,
float leftMarginPercent,
float rightMarginPercent,
float topMarginPercent,
float bottomMarginPercent) {
PercentRelativeLayout.LayoutParams layoutParams = new PercentRelativeLayout.LayoutParams(view.getLayoutParams());
PercentLayoutHelper.PercentLayoutInfo percentLayoutInfo = layoutParams.getPercentLayoutInfo();
percentLayoutInfo.heightPercent = heightPercent;
percentLayoutInfo.widthPercent = widthPercent;
percentLayoutInfo.topMarginPercent= topMarginPercent;
percentLayoutInfo.bottomMarginPercent= bottomMarginPercent;
percentLayoutInfo.leftMarginPercent= leftMarginPercent;
percentLayoutInfo.rightMarginPercent= rightMarginPercent;
view.setLayoutParams(layoutParams);
}
视图如下所示: