在Gridview中的ImageView下方显示文本视图

时间:2013-07-29 10:02:43

标签: android gridview textview imageview

我试试这个:

main.xml中

  <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:id="@+id/mainLayout">
<GridView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:numColumns="2"
android:id="@+id/gridView1_bir">
</GridView>
</LinearLayout>

grid_prueba1.xml

  <?xml version="1.0" encoding="utf-8"?>

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_height="wrap_content"
android:background="#DC0000"
android:orientation="vertical"
android:layout_width="fill_parent"    
android:id="@+id/relaGrid"
 >


<ImageView
    android:id="@+id/img2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

     />

<TextView
    android:id="@+id/txt2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/img2"     

    android:text="TextView"
    android:textColor="#FFFFFF" />

 </RelativeLayout>

ImgeAdapter.java

   package es.ejemplo.gridviewprueba;

 import java.io.FileInputStream;
 import java.io.InputStream;
 import java.net.URI;
 import java.net.URL;

 import android.R.integer;
 import android.app.Activity;
 import android.content.Context;
 import android.graphics.BitmapFactory;
 import android.graphics.drawable.Drawable;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.BaseAdapter;
 import android.widget.GridView;
 import android.widget.ImageView;
 import android.widget.ListAdapter;
 import android.widget.RelativeLayout;
 import android.widget.TextView;

    public class ImageAdapter extends BaseAdapter implements ListAdapter {

private Context context;
public ImageAdapter(Context context) {
    super();
    this.context = context;
    GridViewConfig.addImageUrls();
}

@Override
public int getCount() {
    return GridViewConfig.getResim_list().size();
}

@Override
public Object getItem(int position) {

    return GridViewConfig.getResim_list().get(position);
}

@Override
public long getItemId(int position) {

    return position;
}

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

    View myView;

    if(convertView==null)
    {

        LayoutInflater li = ((Activity)context).getLayoutInflater();
        myView = li.inflate(R.layout.grid_prueba1, null);
        myView.setLayoutParams(new GridView.LayoutParams(100,100));
        myView.setPadding(5,5,5,5);


    }else{

        myView = (View)convertView;

    }

    ImageView img = (ImageView)myView.findViewById(R.id.img2);
        img.setImageDrawable(LoadImageFromURL(GridViewConfig.getResim_list().get(position)));
    TextView txt = (TextView)myView.findViewById(R.id.txt2);
    txt.setText("Adept0000"+position);

    return myView;
}


private Drawable LoadImageFromURL(String url)
{
try
{
InputStream is = (InputStream) new URL(url).getContent();

Drawable d = Drawable.createFromStream(is, "src");
return d;
}catch (Exception e) {
System.out.println(e);
return null;
}
}

   }

GridviewConfig是一个加载图片网址的类。

MyGridView.java

   package es.ejemplo.gridviewprueba;

 import android.app.Activity;
 import android.os.Bundle;
 import android.view.View;

 import android.widget.AdapterView;
 import android.widget.GridView;
 import android.widget.Toast;

public class MyGridView extends Activity {
private GridView girGridView;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    girGridView=(GridView) findViewById(R.id.gridView1_bir);

    girGridView.setAdapter(new ImageAdapter(this));

    girGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) {
            Toast.makeText(getApplicationContext(), GridViewConfig.getResim_list().get(position), Toast.LENGTH_SHORT).show();
        }
    });
}

}

使用此代码构建gridview,我将图片加载到上面的文本中。我假装文本低于图片的描述或标题。

我用texview查看了几个imageview和gridview的例子,但我无法解决我的问题。你能帮帮我吗?

4 个答案:

答案 0 :(得分:2)

可能是你的问题是“运行后TextView不可见(在库样式中显示TextView)”

然后

在图库中显示具有总屏幕的图像时,无法看到剩余的视图。

所以转向画廊认证是正确的选择。

将图像缩放250 * 200(根据您的要求)并将图像设置为imageview。

然后可以看到剩余的视图。

我更改了您的代码

您的gallery_style:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relaGrid"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#DC0000"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/img2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/txt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/img2"
        android:text="TextView"
        android:textColor="#FFFFFF" />

</RelativeLayout>

您的图库适配器getView();

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

        View myView;

        LayoutInflater li = ((Activity) context).getLayoutInflater();
        myView = li.inflate(R.layout.grid_prueba1, null);
        ImageView img = (ImageView) myView.findViewById(R.id.img2);
        TextView txt = (TextView) myView.findViewById(R.id.txt2);

        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.ic_launcher);
        Bitmap bitmap2 = Bitmap.createScaledBitmap(bitmap, 250, 200, true);

        img.setImageBitmap(bitmap2);

        txt.setText("Some text");

        return myView;
    }

enter image description here

答案 1 :(得分:0)

请解释您的问题

我想你可能想要这个

TextView是ImageView的机器人。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/relaGrid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#DC0000"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/img2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/txt2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textColor="#FFFFFF" />

    </LinearLayout>

如果您想使用RelativeLayout,请使用此

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relaGrid"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#DC0000"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/img2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/txt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/img2"
        android:text="TextView"
        android:textColor="#FFFFFF" />

</RelativeLayout>

答案 2 :(得分:0)

好的,谢谢Venky。有了这个我解决了我的问题。

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

    View myView;

    if(convertView==null)
    {

        LayoutInflater li = ((Activity)context).getLayoutInflater();
        myView = li.inflate(R.layout.grid_prueba, null);
        //myView.setLayoutParams(new GridView.LayoutParams(100,100));
        myView.setPadding(5,5,5,5);


    }else{

        myView = (View)convertView;

    }


    ImageView img = (ImageView)myView.findViewById(R.id.img1);
    //img.setImageDrawable(LoadImageFromURL(GridViewConfig.getResim_list().get(position)));
    img.setImageBitmap(getBitmapFromURL(GridViewConfig.getResim_list().get(position)));
    TextView txt = (TextView)myView.findViewById(R.id.txt1);
    txt.setText("Adept0000"+position);

    return myView;
}


public static Bitmap getBitmapFromURL(String src) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        Bitmap myBitmap1 = myBitmap.createScaledBitmap(myBitmap, 250, 200, true);
        return myBitmap1;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

我必须调整图像和文字,但它有效。谢谢大家。

答案 3 :(得分:0)

我修改了 grid_prueba1.xml 文件

所以请将此代码粘贴到grid_prueba1.xml

并查看您的图形布局可能正常工作

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/relaGrid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#DC0000"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/img2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/txt2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/img2"
            android:text="TextView"
            android:textColor="#FFFFFF" />

    </RelativeLayout>