gpt4 book ai didi

java - Android GridView 与图像和文本

转载 作者:行者123 更新时间:2023-12-01 10:25:05 24 4
gpt4 key购买 nike

我需要在 GridView 中的每个图像下方显示文本。我已经能够将图像放入 GridView 中,但如何将文本放在其下方?下面我发布了代码 fragment 。

fragment_facility_grid.xml

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

</GridView>

FacilityAdapter.class

package com.androidbelieve.HIT_APP;

/**
* Created by Akash on 2/13/2016.
*/
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class FacilityAdapter extends BaseAdapter {
private Context mContext;


// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.bank, R.drawable.bus,
R.drawable.girlshostel , R.drawable.guesthouse,
R.drawable.gym , R.drawable.library,R.drawable.sports

};

public String[] mThumbNames = {
"Bank", "Bus Service","Guest House", "GYM","Fac1","Fac2"

};

// Constructor
public FacilityAdapter(Context c){
mContext = c;
}

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

@Override
public Object getItem(int position) {
return mThumbIds[position];
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View gridView;
gridView = new View(mContext);



ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(300, 300));
return imageView;
}

}

谢谢

最佳答案

我宁愿建议您不要在运行时动态创建 ImageView 并从 getView() 方法返回它,而应该创建一个 xml 为您的每个列表项。下面是带有 ImageViewTextView

的示例 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">

<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"/>

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"/>

</LinearLayout>

此外,您还应该遵循 ViewHolder Pattern 来提高 GridView 内存效率。您的 getView() 方法将类似于此

@Override
public View getView(int arg0, View convertView, ViewGroup arg2) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.yourxml,
null);
holder.textview= (TextView) convertView.findViewById(R.id.text);
holder.imageview= (ImageView) convertView.findViewById(R.id.image);
holder.imageView.getLayoutParams().width = yourImageWidth;
holder.imageView.getLayoutParams().height = yourImageHeight;
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

holder.imageView.setImageResource(mThumbIds[position]);
holder.textview.setText(mThumbNames[position]);
return convertView;
}

static class ViewHolder {
ImageView imageView;
TextView textView;
}

关于java - Android GridView 与图像和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35390083/

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