在 Android 中的 GridView 图像上设置文本 [英] Set Text on a GridView Image in Android
本文介绍了在 Android 中的 GridView 图像上设置文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含 8 张图像的网格视图
I have a grid view that have 8 images
现在在特定的图像视图上,我想设置一个将动态更改的文本值
Now on a particular image view i want to set a text value that will be changed dynamically
主要 XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/mainLinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@drawable/blue_bar"
android:orientation="horizontal" >
</LinearLayout>
<GridView
android:id="@+id/gridView1"
android:layout_below="@+id/mainLinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="20dp"
android:columnWidth="100dp"
android:gravity="center"
android:numColumns="2"
android:stretchMode="columnWidth" >
</GridView>
</RelativeLayout>
**内部 XML 布局 **
**Inner XML Layout **
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp" >
<ImageView
android:id="@+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:src="@drawable/ic_launcher" />
</LinearLayout>
代码主要活动
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setAdapter(new ImageAdapter(this, DASHBOARD_LINKS));
适配器类
public class ImageAdapter extends BaseAdapter {
private Context context;
private final String[] dashBoardValues;
public ImageAdapter(Context context, String[] dashBoardValues) {
this.context = context;
this.dashBoardValues = dashBoardValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from dashboard_inner.xml
gridView = inflater.inflate(R.layout.dashboard_inner, null);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
imageView.setImageResource(R.drawable.ic);
} else {
gridView = (View) convertView;
}
return gridView;
}
@Override
public int getCount() {
return dashBoardValues.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
那么我怎么能在网格视图中做到这一点.请帮帮我.
So how could i do this in a grid view .Please help me .
推荐答案
正如其他人已经建议您可以使用相对布局或框架布局
As others have already suggested you can use a Relative Layout or FrameLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="31dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView1"
android:layout_alignTop="@+id/imageView1"
android:layout_marginLeft="20dp"
android:text="TextView" />
</RelativeLayout>
捕捉
检查这个答案 kcoppock 它将让您了解如何使用框架布局
Check this answer kcoppock which will give you an idea of how to use framelayout
放置/重叠(z-index)在android中的另一个视图上方查看
另外最好使用 ViewHolder
模式
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
这篇关于在 Android 中的 GridView 图像上设置文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文