我正在做一个项目,我需要实现一个网格来显示“小部件”(定制 View ),由最终用户动态重新排序。或多或少 与 Android 启动器及其小部件相同,用户可以在网格中移动的小部件(与大多数启动器一样,长按)。这是我正在寻找的内容的预览: Every color represents one different kind of "Widget"
我尝试过使用 GridLayout 和 TableLayout,但我无法“合并”单元格以将布局调整为 Widget。
网格本身是一个 8 x 12 的单元格网格,其中每个 Widget 可以使用超过 1 个单元格,例如,我配置了一个 2 x 3 (h | w) 的“Widget button”。
是否有适合我需要的布局,或者我必须创建自己的布局?
谢谢你,亚伯!
最佳答案
我使用以下代码以编程方式创建 View 并设置它们的 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);
}
关于android - 自定义 View 的不同单元格大小网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36894524/