gpt4 book ai didi

Android:在 gridlayout 或 gridview 中创建具有动态大小的网格

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:30:57 27 4
gpt4 key购买 nike

我打算制作网格基础应用程序,其中网格的数量会根据用户的点击而改变。网格模式:- 2*2、4*4 ... [例如,用户将单击正确的网格,然后网格数量将增加]。我 cn 能够制作网格,但它不适合布局。那么,如何根据用户的点击更改网格的数量以及如何动态设置网格的大小。

这里我附上了我想要实现的图像。

当用户点击不同颜色的网格时,网格数量会增加,如第二张图片。 p>

图片:1

enter image description here

图片:2

用户点击后网格数量增加。

enter image description here

最佳答案

最后我得到了我的问题的答案:

这里需要将 SquareImageView 用于 imageview 并以编程方式设置 gridview 的列,现在看下面的代码:

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
}
}

为查看充气器创建自定义适配器,

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/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) {
}
}

我希望这个答案对任何人都有帮助。

关于Android:在 gridlayout 或 gridview 中创建具有动态大小的网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33314550/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com