实现九宫格布局

前言

实现一个九宫格布局,你能想出几种方法?

基本结构

首先,我们定义好通用的HTML结构:

<div class="box">
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
    </div>

接着是公共样式:

*{
     padding: 0;
     margin: 0;
     box-sizing: border-box;
 }

 div.box {
     width: 400px;
     height: 400px;
     margin: 100px auto;
     padding: 20px;
     border: 5px solid #ffe09c;
 }

 span {
     background: #626277;
 }

实现

1. flex实现

.box {
   display: flex;
   flex-wrap: wrap;
   justify-content: space-between;
    align-content: space-between;
}

.box span {
    width: 32%;
    height: 32%;
} 

2. grid实现

 .box {
    display: grid;
    gap: 10px 10px;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
  }

提示:grid-template-areas: ‘a a a’ ‘b b b’ ‘c c c’; 可以代替后两行

3. float实现

 .box span {
    float: left;
    width: 32%;
    height: 32%;
    margin: 0 2% 2% 0;
}
.box span:nth-child(3n) {
   margin-right: 0;
}
.box span:nth-child(n+7) {
   margin-bottom: 0;
}

注意:如果父盒子没有固定高度记得清除浮动!不然会影响其他布局

4. inline-block实现

.box {
    font-size: 0;
}
.box span {
    display: inline-block;
    width: 32%;
    height: 32%;
    margin: 0 2% 2% 0;
}
.box span:nth-child(3n) {
    margin-right: 0;
}
 .box span:nth-child(n+7) {
    margin-bottom: 0;
}

注意:span与span会存在间距,可以在父盒子用font-size:0;或者letter-spacing: -10px;等来消除间距

5. table实现
这个方法跟其他方法相比可能有点特殊,所以我直接贴全部代码!

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>九宫格实现</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }

        div.box {
            margin: 100px auto;
            width: 400px;
            height: 400px;
            border: 5px solid #ffe09c;
        }

        span {
            background: #626277;
        }

        /* table实现 */
        .box {
            display: table;
            border-spacing: 10px;
        }

        .row {
            display: table-row;
        }

        .row span {
            display: table-cell;
            width: 30%;
            height: 30%;
        }
    </style>
</head>

<body>
<div class="box">
        <div class="row"><span></span><span></span><span></span></div>
        <div class="row"><span></span><span></span><span></span></div>
        <div class="row"><span></span><span></span><span></span></div>
    </div>
</body>

</html>

不过好像也可以利用css的display中的table属性来实现,那就交给你们啦!

最终效果

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值