在Android中实现九宫格布局
时间: 2024-05-08 21:15:12 浏览: 167
Android中可以使用GridLayout或者自定义布局实现九宫格布局。
1. 使用GridLayout
GridLayout是Android提供的布局,可以实现网格布局。以下是一个九宫格布局的例子:
```
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="3"
android:columnCount="3"
android:orientation="horizontal">
<Button
android:text="Button 1"
android:layout_row="0"
android:layout_column="0"/>
<Button
android:text="Button 2"
android:layout_row="0"
android:layout_column="1"/>
<Button
android:text="Button 3"
android:layout_row="0"
android:layout_column="2"/>
<Button
android:text="Button 4"
android:layout_row="1"
android:layout_column="0"/>
<Button
android:text="Button 5"
android:layout_row="1"
android:layout_column="1"/>
<Button
android:text="Button 6"
android:layout_row="1"
android:layout_column="2"/>
<Button
android:text="Button 7"
android:layout_row="2"
android:layout_column="0"/>
<Button
android:text="Button 8"
android:layout_row="2"
android:layout_column="1"/>
<Button
android:text="Button 9"
android:layout_row="2"
android:layout_column="2"/>
</GridLayout>
```
2. 自定义布局
也可以使用自定义布局实现九宫格布局。以下是一个自定义九宫格布局的例子:
```
public class NineGridLayout extends LinearLayout {
private static final int DEFAULT_COLUMN_COUNT = 3;
private int mColumnCount;
private List<View> mViewList;
public NineGridLayout(Context context) {
super(context);
init();
}
public NineGridLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public NineGridLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
setOrientation(VERTICAL);
mViewList = new ArrayList<>();
mColumnCount = DEFAULT_COLUMN_COUNT;
}
public void setColumnCount(int columnCount) {
mColumnCount = columnCount;
}
public void setViews(List<View> viewList) {
mViewList.clear();
mViewList.addAll(viewList);
notifyDataSetChanged();
}
public void notifyDataSetChanged() {
removeAllViews();
int rowCount = (int) Math.ceil(mViewList.size() * 1.0 / mColumnCount);
for (int i = 0; i < rowCount; i++) {
LinearLayout rowLayout = new LinearLayout(getContext());
rowLayout.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
rowLayout.setOrientation(HORIZONTAL);
for (int j = 0; j < mColumnCount; j++) {
int index = i * mColumnCount + j;
if (index < mViewList.size()) {
View view = mViewList.get(index);
view.setLayoutParams(new LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
rowLayout.addView(view);
} else {
View emptyView = new View(getContext());
emptyView.setLayoutParams(new LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
rowLayout.addView(emptyView);
}
}
addView(rowLayout);
}
}
}
```
使用时可以在XML中定义布局:
```
<com.example.customview.NineGridLayout
android:id="@+id/nine_grid_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:columnCount="3"/>
```
然后在代码中设置View:
```
List<View> viewList = new ArrayList<>();
for (int i = 0; i < 9; i++) {
Button button = new Button(this);
button.setText("Button " + (i + 1));
viewList.add(button);
}
NineGridLayout nineGridLayout = findViewById(R.id.nine_grid_layout);
nineGridLayout.setViews(viewList);
```
阅读全文