当前位置:首页 > 行业动态 > 正文

如何实现Android应用中的九宫格布局?

Android九宫格源码通常涉及自定义的GridView或RecyclerView,以及相应的适配器和布局。

九宫格是一种常见的布局方式,在Android中可以使用GridView或者RecyclerView来实现,这里以GridView为例,给出一个简单的九宫格源码示例:

如何实现Android应用中的九宫格布局?  第1张

1、首先在布局文件(如activity_main.xml)中添加GridView:

<?xml version="1.0" encoding="utf8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/resauto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <GridView
        android:id="@+id/grid_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:numColumns="3"
        android:stretchMode="columnWidth" />
</LinearLayout>

2、创建一个自定义的Adapter类(如GridAdapter.java):

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class GridAdapter extends BaseAdapter {
    private List<String> mData;
    private Context mContext;
    private int mResource;
    public GridAdapter(Context context, int resource, List<String> data) {
        this.mContext = context;
        this.mResource = resource;
        this.mData = data;
    }
    @Override
    public int getCount() {
        return mData.size();
    }
    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(mResource, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.image_view);
            viewHolder.textView = (TextView) convertView.findViewById(R.id.text_view);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.textView.setText(mData.get(position));
        // 根据需要设置图片资源
        viewHolder.imageView.setImageResource(R.drawable.icon);
        return convertView;
    }
    class ViewHolder {
        ImageView imageView;
        TextView textView;
    }
}

3、在Activity中设置Adapter(如MainActivity.java):

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.GridView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
    private GridView mGridView;
    private GridAdapter mAdapter;
    private List<String> mData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mGridView = (GridView) findViewById(R.id.grid_view);
        mData = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            mData.add("Item " + (i + 1));
        }
        mAdapter = new GridAdapter(this, R.layout.grid_item, mData);
        mGridView.setAdapter(mAdapter);
    }
}

4、创建grid_item.xml布局文件:

<?xml version="1.0" encoding="utf8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="10dp">
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon" />
    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:textColor="#000" />
</LinearLayout>

这样就实现了一个简单的九宫格布局,你可以根据需要修改代码以满足你的需求。

小伙伴们,上文介绍了“android 九宫格源码”的内容,你了解清楚吗?希望对你有所帮助,任何问题可以给我留言,让我们下期再见吧。

0