问题描述
限时送ChatGPT账号..我打算制作基于网格的应用程序,其中网格的数量会根据用户的点击而改变.网格模式:- 2*2 , 4*4 ... [喜欢,用户将点击正确的网格,然后网格的数量会增加].我可以制作网格,但它不适合布局.那么,如何在用户点击时更改网格的数量以及如何动态设置网格的大小.
这里我附上了我想要实现的图像.
当用户点击不同颜色的网格时,网格数量会增加,就像第二张图片.>
图片:1
图片:2
用户点击时增加的网格数.
解决方案我的问题终于得到了答案:
这里需要为imageview取SquareImageView并以编程方式设置gridview的列,现在看下面的代码:
SquareImageView.java
public class SquareImageView extends ImageView {公共 SquareImageView(上下文上下文){超级(上下文);}公共 SquareImageView(上下文上下文,AttributeSet attrs){超级(上下文,属性);}公共 SquareImageView(上下文上下文,AttributeSet attrs,int defStyle){超级(上下文,属性,defStyle);}@覆盖protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());//捕捉到//宽度}}
为视图充气机创建自定义适配器,
layout.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"机器人:方向=垂直"><com.example.SquareImageViewandroid:id="@+id/image_grid"android:layout_width="match_parent"android:layout_height="match_parent" ></com.example.SquareImageView></LinearLayout>
MainActivity.java
public class MainActivity extends Activity 实现 OnClickListener {整数计数器 = 1;自定义网格适配器;静态 int 列 = 2;公共静态 GridView 网格视图;@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);适配器 = 新 CustomGrid(MainActivity.this);gridview = (GridView) findViewById(R.id.gridview);gridview.setNumColumns(2);gridview.setAdapter(适配器);}公共类 CustomGrid 扩展 BaseAdapter {私有上下文 mContext;CustomGrid adgg;int mNumColumns = 2;私人随机兰特 = 新随机();GridView gg;int color = Color.argb(255, rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));浮动阿尔法 = 0.1f;int rand_ind = rand.nextInt((2 * 2) - 0) + 0;公共自定义网格(上下文 c){mContext = c;//this.Imageid = Imageid;//this.web = web;}@覆盖公共 int getCount() {//TODO 自动生成的方法存根返回 mNumColumns * mNumColumns;}@覆盖公共对象 getItem(int position) {//TODO 自动生成的方法存根返回空;}@覆盖public long getItemId(int position) {//TODO 自动生成的方法存根返回0;}@覆盖public View getView(final int position, View convertView, ViewGroup parent) {//TODO 自动生成的方法存根查看网格视图;LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);视图网格 = 新视图(mContext);grid = inflater.inflate(R.layout.layout, null);最终视图 imageView = (ImageView) grid.findViewById(R.id.image_grid);if (convertView == null) {如果(位置 == rand_ind){imageView.setBackgroundColor(getColorWithAlpha(color, alfa));} 别的 {imageView.setBackgroundColor(颜色);}} 别的 {网格=(视图)convertView;}grid.setOnClickListener(new OnClickListener() {@覆盖public void onClick(View v) {如果(位置 == rand_ind){//如果条件发生,则更正位置点击时间..........如果(计数器 == 2){mNumColumns = mNumColumns + 1;阿尔法 = 阿尔法 + 0.1f;} else if (计数器 == 6) {mNumColumns = mNumColumns + 1;阿尔法 = 阿尔法 + 0.08f;} else if (计数器 == 8) {mNumColumns = mNumColumns + 1;阿尔法 = 阿尔法 + 0.1f;} else if (计数器 == 12) {mNumColumns = mNumColumns + 1;阿尔法 = 阿尔法 + 0.1f;} else if (计数器 == 14) {mNumColumns = mNumColumns + 1;阿尔法 = 阿尔法 + 0.1f;} else if (计数器 == 16) {mNumColumns = mNumColumns + 1;阿尔法 = 阿尔法 + 0.1f;} else if (计数器 == 18) {mNumColumns = mNumColumns + 1;阿尔法 = 阿尔法 + 0.05f;} else if (counter >= 18) {//mNumColumns = mNumColumns + 1;阿尔法 = 0.8f;}gridview.setNumColumns(mNumColumns);gridview.setAdapter(适配器);适配器.notifyDataSetChanged();rand_ind = rand.nextInt((mNumColumns * mNumColumns) - 0) + 0;} 别的 {//这里是负点击的代码…………whem click//否定时间 geanarate 这段代码.........}}});返回网格;}public int getColorWithAlpha(int color, float ratio) {int newColor = 0;int alpha = Math.round(Color.alpha(color) * ratio);int r = Color.red(颜色);int g = Color.green(颜色);int b = Color.blue(color);newColor = Color.argb(alpha, r, g, b);返回新颜色;}}@覆盖public void onClick(View v) {}}
我希望这个答案对任何人都有帮助.
I am planning to make grid base application in which number of grids will change according to user's click. grid pattern:- 2*2 , 4*4 ... [like, user will click on correct grid then number of grid will increase]. I cn able to make grid but it is not fit in layout. so, how can i change the number of grid on user's click and how can i set size of grid dynamically.
Here i have attached the image which i want to achieve .
When user click on different color grid, Number of grid will increase like second image.
Image : 1
Image : 2
Number of grid increased on user's click.
解决方案Finally i got answer of My question:
Here need to take SquareImageView for imageview and programatically set column of gridview, now see below code:
SquareImageView.java
public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); // Snap to
// width
}
}
Create Custome adapter for view inflator,
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.SquareImageView
android:id="@+id/image_grid"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.example.SquareImageView>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity implements OnClickListener {
int counter = 1;
CustomGrid adapter;
static int colum = 2;
public static GridView gridview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new CustomGrid(MainActivity.this);
gridview = (GridView) findViewById(R.id.gridview);
gridview.setNumColumns(2);
gridview.setAdapter(adapter);
}
public class CustomGrid extends BaseAdapter {
private Context mContext;
CustomGrid adgg;
int mNumColumns = 2;
private Random rand = new Random();
GridView gg;
int color = Color.argb(255, rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
float alfa = 0.1f;
int rand_ind = rand.nextInt((2 * 2) - 0) + 0;
public CustomGrid(Context c) {
mContext = c;
// this.Imageid = Imageid;
// this.web = web;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mNumColumns * mNumColumns;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View gridView;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View grid = new View(mContext);
grid = inflater.inflate(R.layout.layout, null);
final View imageView = (ImageView) grid.findViewById(R.id.image_grid);
if (convertView == null) {
if (position == rand_ind) {
imageView.setBackgroundColor(getColorWithAlpha(color, alfa));
} else {
imageView.setBackgroundColor(color);
}
} else {
grid = (View) convertView;
}
grid.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (position == rand_ind) {
// Correct postion click time if condition occur..........
if (counter == 2) {
mNumColumns = mNumColumns + 1;
alfa = alfa + 0.1f;
} else if (counter == 6) {
mNumColumns = mNumColumns + 1;
alfa = alfa + 0.08f;
} else if (counter == 8) {
mNumColumns = mNumColumns + 1;
alfa = alfa + 0.1f;
} else if (counter == 12) {
mNumColumns = mNumColumns + 1;
alfa = alfa + 0.1f;
} else if (counter == 14) {
mNumColumns = mNumColumns + 1;
alfa = alfa + 0.1f;
} else if (counter == 16) {
mNumColumns = mNumColumns + 1;
alfa = alfa + 0.1f;
} else if (counter == 18) {
mNumColumns = mNumColumns + 1;
alfa = alfa + 0.05f;
} else if (counter >= 18) {
// mNumColumns = mNumColumns + 1;
alfa = 0.8f;
}
gridview.setNumColumns(mNumColumns);
gridview.setAdapter(adapter);
adapter.notifyDataSetChanged();
rand_ind = rand.nextInt((mNumColumns * mNumColumns) - 0) + 0;
} else {
// Here code for nagative click............ whem click
// nagative at time geanarate this code.........
}
}
});
return grid;
}
public int getColorWithAlpha(int color, float ratio) {
int newColor = 0;
int alpha = Math.round(Color.alpha(color) * ratio);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
newColor = Color.argb(alpha, r, g, b);
return newColor;
}
}
@Override
public void onClick(View v) {
}
}
I hope this answer help full to any one.
这篇关于Android:在 gridlayout 或 gridview 中创建具有动态大小的网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论