如何在Android中将屏幕划分为小方块?

时间:2018-09-18 07:43:34

标签: android android-layout

我想使所有屏幕都分成小方块,当我触摸其中一个时,它会改变我触摸的方块的颜色。 我没有找到执行此操作的任何代码。 我该如何在android中做到这一点?

3 个答案:

答案 0 :(得分:1)

使用GridView实现这种事情

GridView

GridView是一个ViewGroup,它在二维可滚动网格中显示项目。使用ListAdapter将网格项自动插入到布局中。

将Click Listener设置为特定项目,以更改相应的框颜色。

供参考:-{https://developer.android.com/guide/topics/ui/layout/gridview

答案 1 :(得分:0)

您应该使用RecyclerView来回收其子视图以提高性能,并且可以将其layoutmanager设置为GridLayoutManger。 对于颜色更改,您可以在初始化数据列表并单击项目时更新颜色时将颜色或状态存储到诸如SparseArray之类的颜色中,然后在onBindViewHolder()方法中从您的视图保存器getAdapterPosition()中检索正确的颜色。从您的Recyclerview.Adapter

以下是参考文献:

https://developer.android.com/reference/android/support/v7/widget/RecyclerView https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter https://developer.android.com/reference/android/support/v7/widget/GridLayoutManager

答案 2 :(得分:0)

XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradient_touchscreen"
    tools:context=".tochscreen_page_2">


    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="90dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="none"
        android:gravity="center"
        />




</android.support.constraint.ConstraintLayout>

Java

public class tochscreen_page_2 extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tochscreen_page_2);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));

        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {
                Toast.makeText(tochscreen_page_2.this, "" + position,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

ImageAdapter

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.gradient_charger, R.drawable.gradient_flashlight,
            R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
            R.drawable.gradient_charger, R.drawable.gradient_flashlight,
            R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
            R.drawable.gradient_charger, R.drawable.gradient_flashlight,
            R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
            R.drawable.gradient_charger, R.drawable.gradient_flashlight,
            R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
            R.drawable.gradient_charger, R.drawable.gradient_flashlight,
            R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
            R.drawable.gradient_charger, R.drawable.gradient_flashlight,
            R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
            R.drawable.gradient_charger, R.drawable.gradient_flashlight,
            R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
            R.drawable.gradient_charger, R.drawable.gradient_flashlight,
            R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
            R.drawable.gradient_charger, R.drawable.gradient_flashlight,
            R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
            R.drawable.gradient_charger, R.drawable.gradient_flashlight,
            R.drawable.gradient_vibrtion, R.drawable.gradient_charger,













    };
}

这是结果: http://prntscr.com/kvqe34

相关问题