在Android中的GridView图像上设置文本

时间:2013-12-30 07:20:56

标签: android android-layout android-gridview android-gridlayout

我的网格视图有8张图片like this

现在,在特定的图片视图中,我想设置一个动态更改的文本值Like This

主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布局**

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

}

那我怎么能在网格视图中这样做。请帮助我。

6 个答案:

答案 0 :(得分:2)

正如其他人已经建议您可以使用相对布局或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>

对齐

enter image description here

检查此答案kcoppock,它将让您了解如何使用framelayout

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

最好使用ViewHolder模式

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

答案 1 :(得分:1)

这是实际工作代码

public class MainActivity extends Activity {

    private static final String TAG = "AAA";

    private static Handler mHandler;

    private LayoutInflater layoutInflater;
    private GridView mGridView;
    private int click = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        layoutInflater = LayoutInflater.from(getApplicationContext());

        click = 0;

        setContentView(R.layout.my_grid_view);
        mGridView = (GridView)findViewById(R.id.my_grid_view);
        mGridView.setAdapter(new MyAdapter());
        mGridView.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> adapterViews, View pView, int position, long id) {

                click++;
                TextView textView = (TextView)pView.findViewById(R.id.my_text_view);
                if(textView!=null){
                    textView.setText("Changed : Position = " + position + " : Click No = " + click);
                }else{
                    Log.d(TAG, "Fish : textView = " + textView);
                }
            }
        });

    }


    private class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return 200;
        }

        @Override
        public Object getItem(int arg0) {
            return arg0;
        }

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

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

            if(convertView == null){ //This is memory efficient, but after certain no of time u will get similar view repetition, 
                //to over come that overwrite getViewTypeCount() & provide proper way of persistent view & data management mechanism.
                convertView = layoutInflater.inflate(R.layout.child_one, null);
            }

            return convertView;
        }

    }}

和XMLs

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

并且

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/my_image_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:scaleType="fitXY"
        android:src="@drawable/flower_1" />

    <TextView
        android:id="@+id/my_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top|right"
        android:background="@android:color/darker_gray"
        android:text="Default Text" />

</FrameLayout>

答案 2 :(得分:0)

代替图像视图传递framelayout参考。它会工作。     framelayout是视图组,只有子视图类。

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:gravity="center"
    android:orientation="vertical">

<ImageView android:scaleType="fitXY"
    android:src="@drawable/ic_launcher"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/image_view"
    android:layout_gravity="center"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_gravity="top|right"
    android:background="@android:color/darker_gray"
    android:layout_height="wrap_content"
    android:id="@+id/badge_view"
    android:text="10"/>
</FrameLayout>
快照

enter image description here

答案 3 :(得分:0)

获取此图片并添加到您的项目中。 new http://www.flickr.com/photos/113556524@N05/11793569304/

这是屏幕截图nw http://www.flickr.com/photos/113556524@N05/11793569304/

这是您要使用的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" 
    android:weightSum="4">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_weight="1"
        android:weightSum="2">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/aaa"
            android:text="" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/aaa"
            android:text="" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_weight="1"
        android:weightSum="2">

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/aaa"
            android:text="" />

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/aaa"
            android:text="" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:weightSum="2">

        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/aaa"
            android:text="" />

        <Button
            android:id="@+id/button6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/aaa"
            android:text="" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:weightSum="2">

        <Button
            android:id="@+id/button7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/aaa"
            android:text="" />

        <Button
            android:id="@+id/button8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/aaa"
            android:text="" />

    </LinearLayout>



</LinearLayout>

在你的活动java文件中使用:

package com.example.dashboard;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    Button b1,b2,b4,b3,b5,b6,b7,b8;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.neww);

        b1 =(Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        b3 = (Button) findViewById(R.id.button3);
        b4 = (Button) findViewById(R.id.button4);
        b5 = (Button) findViewById(R.id.button5);
        b6 = (Button) findViewById(R.id.button6);
        b5 = (Button) findViewById(R.id.button7);
        b6 = (Button) findViewById(R.id.button8);




        /* Wat ever you want validation , anything do here */

       // if you need to set text to button 

        b1.setText("Hi pooja");


        //  blah blah blah - wat ever you want to any button text



    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

得到了;)

答案 4 :(得分:0)

使用FrameLayout作为GridView的子级。在FrameLayout中首先放入ImageView&amp;在它上面放了一个像“sush”所提到的TextView。 然后你可以改变Visibility&amp; TextView按需内容,通过程序。 它不适合你,因为你可能已经把所有TextViews都放在同一个“android:id”中,为不同的TextView添加了不同的id。它必将奏效。我已经测试了。

答案 5 :(得分:0)

http://i.stack.imgur.com/HUY4o.png Check the sample image...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/album_item"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/cover"
    android:layout_width="148dp"
    android:layout_height="148dp"
    android:src="@drawable/empty_photo" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/cover"
    android:background="#70000000"
    android:padding="6dp" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textSize="12sp"
        android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>

---&GT;使用此布局并在适配器中对其进行充气。您将获得图像底部的文本。