gpt4 book ai didi

android gallery 变成网格样式的菜单

转载 作者:行者123 更新时间:2023-11-29 02:13:37 26 4
gpt4 key购买 nike

大家好,请帮我解决一下 android 中的菜单问题。我想将此画廊作为我的应用程序的网格菜单。请指导我如何将名称放在图像下,如果 iclick 在特定图像上,新 Activity 应该打开,最后单击菜单按钮将我带回主屏幕

这是图片 enter image description here

这是代码

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class DisplayViewsExample extends Activity
{
//---the images to display---
Integer[] imageIDs = {
R.drawable.pic1,
R.drawable.pic2,
R.drawable.pic3,
R.drawable.pic4,
R.drawable.pic5,
R.drawable.pic6,
R.drawable.pic7
};

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

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(getBaseContext(),
"pic" + (position + 1) + " selected",
Toast.LENGTH_SHORT).show();
}
});
}

public class ImageAdapter extends BaseAdapter
{
private Context context;

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

//---returns the number of images---
public int getCount() {
return imageIDs.length;
}

//---returns the ID of an item---
public Object getItem(int position) {
return position;
}

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

//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5, 5, 5, 5);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(imageIDs[position]);
return imageView;
}
}
}

这是xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>

最佳答案

您需要使用自定义适配器而不是 ImageAdapter,并添加带有图像和标签的垂直 LinearLayout。查看 BaseAdapter api:http://developer.android.com/reference/android/widget/BaseAdapter.html

对于点击事件,使用OnItemClickAdapter:http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html

关于android gallery 变成网格样式的菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5782922/

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