自定义视图的不同单元格大小网格

时间:2016-04-27 15:38:43

标签: android view android-gridview dynamically-generated android-gridlayout

我正在开发一个项目,我需要实现一个网格来显示“Widgets”(自定义视图),由最终用户动态重新排序。或多或少与Android Launchers及其小部件相同,可由用户通过网格移动的小部件(长按,与大多数启动器一样)。这是我正在寻找的预览: Every color represents one different kind of "Widget"

我尝试过使用GridLayout和TableLayout,但是我无法“合并”单元格来调整Widget的布局。

网格本身是一个8 x 12的单元格网格,其中Widgets每个可以使用1个以上的单元格,例如,我已经配置了一个2 x 3(h | w)的“Widget按钮”。

是否有适合我需要的布局,或者我必须创建自己的布局?

谢谢你,阿贝尔!

2 个答案:

答案 0 :(得分:1)

您的应用可以使用带有回收站视图的staggeredGridLayout吗? http://developer.android.com/reference/android/support/v7/widget/StaggeredGridLayoutManager.html

所有列大小必须相同,但......有点像以下enter image description here

取自此示例https://guides.codepath.com/android/using-the-recyclerview

答案 1 :(得分:1)

我使用以下代码以编程方式创建视图并设置其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);

}

视图如下所示: enter image description here