1

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

我试过用GridLayout和TableLayout,但我不能“合并”单元格来调整布局到窗口小部件。

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

有没有适合我需要的布局或我必须创建我自己的布局?

谢谢亚伯!

+0

你看过PercentRelativeLayout吗? – ditkin

+0

它看起来很有前途!我会尝试配置一些内容并再次发表评论! **编辑:**我试图在Java中配置PercentRelativeLayout孩子的百分比,但它似乎(看我发现的例子:[example](http://stackoverflow.com/questions/32497833/how-to-set-如果使用XML定义子View,百分比只能通过Java解决。我需要动态地创建和定位对象,就像启动器窗口小部件一样。 谢谢! –

回答

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

+0

我将这个标记为正确的答案,因为它对于我所问的内容来说似乎完全有效,但我想我找到了一种方法来用一个简单的GridLayout和一些用于管理位置的数组来完成它。如果终于有效,我会在这里发布。 谢谢! –

+0

当您发布解决方案时,请随意将其标记为正确的。我想证明您可以通过编程方式使用布局,并获得您想要的行为。 – ditkin

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

+0

嗯...列大小应该是可变的,所以我认为不是最好的选择...我会看看这个,但它似乎不适合我的东西 无论如何,谢谢你你的支持! –

相关问题