说我想要3行3列按钮,总共9个按钮。如果任何位置的一个按钮被移除,我将如何移动它们以便按钮曾经没有间隙?我可以想到在一项活动中实现这一目标的一些冗长且可能不必要的复杂方法。有没有更简单的方法通过布局文件来做到这一点?
答案 0 :(得分:0)
我不确定您是否可以在Android中完全通过XML代码执行此操作,但您可以使用RecyclerView:
RecylerView recycler = findViewById(R.id.recyclerInActivity);
//now create a GridLayoutManager
GridLayoutManager gridLayoutManager = new GridLayoutManager(this);
//set the gridLayoutManager's spanCount to 3 for 3 columns:
gridLayoutManager.setSpanCount(3);
//Now create your custom Adapter with an XML layout with a button and set the adapter
recycler.setAdapter(myCustomAdapter);
//When a view is removed, remove it from the data supply (e.g. arrayListOfData.remove(indexOfRemovedItem)) of the RecylerView.Adapter and then call:
myCustomAdapter.notifyItemChanged(indexOfRemovedItem);
RecyclerView内置的动画将确保RecyclerView的布局适应新的项目数