android开发中的九宫格布局的实现

九宫格布局的实现,话不多说了,直接上代码:

activity_main.xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >


    <GridView
        android:id="@+id/gv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:columnWidth="90dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="3"
        android:stretchMode="columnWidth"
        android:verticalSpacing="30dp" >
    </GridView>


</RelativeLayout>

适配器中的适配布局gridview_item.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:layout_gravity="center_horizontal">


    <ImageView
        android:id="@+id/iv_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />


    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/iv_image"
       android:layout_centerHorizontal="true"
        android:text="标题" />


</RelativeLayout>


MainActivity.java文件,实现九宫格的代码填充:

import java.util.ArrayList;
import java.util.List;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;


public class MainActivity extends Activity {

private GridView gv;

private String[] title = { "第一格", "第二格", "第三格", "第四格", "第五格", "第六格", "第七格", "第八格", "第九格" };
private int[] image = { R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv = (GridView) findViewById(R.id.gv);
MyAdapter adapter = new MyAdapter(this);
gv.setAdapter(adapter);
}

class MyAdapter extends BaseAdapter {// 创建适配器
private List<PicEntity> list = new ArrayList<>();
private Context context;


public MyAdapter(Context context) {
this.context = context;
for (int i = 0; i < image.length; i++) {
PicEntity picEntity = new PicEntity(title[i], image[i]);
list.add(picEntity);
}
}

@Override
public int getCount() {
return list != null ? list.size() : 0;
}

@Override
public Object getItem(int position) {
return list.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
Viewholder vh;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.gridview_item, null);
vh = new Viewholder();
vh.iv_pic = (ImageView) convertView.findViewById(R.id.iv_image);
vh.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
convertView.setTag(vh);
} else {
vh = (Viewholder) convertView.getTag();
}
vh.iv_pic.setImageResource(list.get(position).imageId);
vh.tv_title.setText(list.get(position).title);
return convertView;
}
}

class Viewholder {
TextView tv_title;
ImageView iv_pic;
}

class PicEntity {// 创建实体类
private String title;
private int imageId;

public PicEntity() {
super();
}

public PicEntity(String title, int imageId) {
super();
this.title = title;
this.imageId = imageId;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public int getImageId() {
return imageId;
}

public void setImageId(int imageId) {
this.imageId = imageId;
}
}
}


运行效果:


源码地址:http://download.csdn.net/detail/zjp776/9491250

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值