在Android的一个GridView图像设置文本 [英] Set Text on a GridView Image in Android

查看:115
本文介绍了在Android的一个GridView图像设置文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网格视图有8个图像

现在在一个特定的图像视图我想设置将被动态地改变 <文本值/ P>

主要XML

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT; RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=横向&GT;

    &LT;的LinearLayout
        机器人:ID =@ + ID / mainLinearLayout
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentTop =真
        机器人:背景=@可绘制/ blue_bar
        机器人:方向=横向&GT;
    &LT; / LinearLayout中&GT;

    &LT; GridView控件
        机器人:ID =@ + ID / gridView1
        机器人:layout_below =@ + ID / mainLinearLayout
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:layout_marginTop =20dp
        机器人:columnWidth中=100dp
        机器人:重力=中心
        机器人:为numColumns =2
        机器人:stretchMode =columnWidth中&GT;
    &LT; / GridView的&GT;

&LT; / RelativeLayout的&GT;
 

*的内蒙古XML布局*

 &LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:填充=5DP&GT;

    &LT; ImageView的
        机器人:ID =@ + ID / grid_item_image
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginLeft =20dp
        机器人:layout_marginRight =10dp
        机器人:SRC =@可绘制/ ic_launcher/&GT;

&LT; / LinearLayout中&GT;
 

C主要活动的 $ C $

 的GridView =(GridView控件)findViewById(R.id.gridView1);
gridView.setAdapter(新ImageAdapter(这一点,DASHBOARD_LINKS));
 

适配器类

 公共类ImageAdapter扩展了BaseAdapter {
    私人上下文的背景下;
    私人最终的String [] dashBoardValues​​;

    公共ImageAdapter(上下文的背景下,的String [] dashBoardValues​​){
        this.context =背景;
        this.dashBoardValues​​ = dashBoardValues​​;
    }

    公共查看getView(INT位置,查看convertView,ViewGroup中父){

        LayoutInflater充气=(LayoutInflater)上下文
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        查看GridView控件;

        如果(convertView == NULL){

            GridView控件=新景(背景);

            //从dashboard_inner.xml布局
            GridView控件= inflater.inflate(R.layout.dashboard_inner,NULL);
            //根据选定的文本集图像
            ImageView的ImageView的=(ImageView的)GridView控件
                    .findViewById(R.id.grid_item_image);
            imageView.setImageResource(R.drawable.ic);


        } 其他 {
            GridView控件=(查看)convertView;
        }

        返回GridView控件;
    }

    @覆盖
    公众诠释getCount将(){
        返回dashBoardValues​​.length;
    }

    @覆盖
    公共对象的getItem(INT位置){
        返回null;
    }

    @覆盖
    众长getItemId(INT位置){
        返回0;
    }

}
 

所以,我怎么能做到这一点的网格视图。请帮助我。

解决方案

正如其他人已经建议你可以用一个相对布局或的FrameLayout

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT; RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent&GT;

    &LT; ImageView的
        机器人:ID =@ + ID / imageView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentTop =真
        机器人:layout_centerHorizo​​ntal =真
        机器人:layout_marginTop =31dp
        机器人:SRC =@可绘制/ ic_launcher/&GT;

    &LT;的TextView
        机器人:ID =@ + ID / textView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignLeft =@ + ID / imageView1
        机器人:layout_alignTop =@ + ID / imageView1
        机器人:layout_marginLeft =20dp
        机器人:文本=TextView的/&GT;

&LT; / RelativeLayout的&GT;
 

检查这个答案kcoppock,这将给你如何使用的FrameLayout的想法

<一个href="http://stackoverflow.com/questions/4182486/placing-overlappingz-index-a-view-above-another-view-in-android">Placing/Overlapping(z-index)以上的Andr​​oid 另一个图的图

另外最好是使用 ViewHolder 模式

http://developer.android.com/training/improving-布局/平滑scrolling.html

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

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

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

Code Main Activity

gridView = (GridView) findViewById(R.id.gridView1);
gridView.setAdapter(new ImageAdapter(this, DASHBOARD_LINKS));

Adapter Class

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>

Snap

Check this answer kcoppock which will give you an idea of how to use framelayout

Placing/Overlapping(z-index) a view above another view in android

Also it is better to use a ViewHolder Pattern

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

这篇关于在Android的一个GridView图像设置文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆