简体   繁体   English

Android网格视图在每个网格下方显示标题

[英]android grid view display caption below each grid

I want to display some text below each grid image. 我想在每个网格图像下方显示一些文本。

Please see my code below. 请在下面查看我的代码。 Please help me in finding why its not working I am getting the images properly displayed but no text if i try to display text within image its coming so it should be something with the layout defined. 请帮助我找到为什么它不起作用的原因,如果我尝试在即将到来的图像中显示文本,则无法正确显示图像,但是没有文本,因此应该是已定义布局的内容。 but i am not able to debug 但我无法调试

adaptor code : 适配器代码:

 public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.griditems, null);
        holder = new ViewHolder();
    holder.text1 = (TextView) convertView
                .findViewById(R.id.grid_item_text);
    Log.i(TAG, holder.text1.toString());
        holder.image = (ImageView) convertView
                .findViewById(R.id.grid_item_image);


            // if it's not recycled, initialize some attributes
            holder.image.setImageResource(gridItemIds[position]);
            holder.text1.setText(gridTitles[position]);
        //holder.image.setScaleType(ImageView.ScaleType.FIT_XY);
        // holder.image.setPadding(20, 20, 20, 20);

    convertView.setLayoutParams(new GridView.LayoutParams(Utils
                .getLayoutParameter(), Utils.getLayoutParameter()));

        holder.image.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

            }
        });
        convertView.setTag(holder);
    } else {

        holder = (ViewHolder) convertView.getTag();

    }

    return convertView;
}

static class ViewHolder {
    TextView text1;
    ImageView image;

}



    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/GridItem"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="vertical"
   android:gravity="center_horizontal"
  >

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

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

</LinearLayout>

gridview : 网格视图 :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">


        <TextView android:id="@+id/text1view" android:textStyle="bold"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:gravity="center_horizontal" android:background="@drawable/title_bar" 
            />

        <GridView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/gridview" android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:verticalSpacing="50dip"
            android:horizontalSpacing="10dp" android:numColumns="3"
            android:stretchMode="columnWidth"
            android:layout_below="@+id/text1view" 
                         android:gravity="center"
            android:layout_centerHorizontal="true" 
            android:background="@drawable/home_bg"
                        />


</LinearLayout>

Also try using RelativeLayout too, placing the TextView underneath the ImageView by using 也可以尝试使用RelativeLayout,将TextView通过以下方式放置在ImageView的下面

 android:layout_below="@id/grid_item_image"

It'll place it underneath the image, however, if you have the option to use the Graphical Layout, dragging it where you want it in conjunction with the image will set all the parameters for you instead of typing them out. 它将放置在图像下方,但是,如果您可以选择使用“图形布局”,则将其拖动到与图像结合使用的位置将为您设置所有参数,而不是键入它们。

I used your xml but with a simple array adapter that I wrote and everything work perfectly, try to replace your adapter with mine and let me know, thanks. 我使用了您的xml,但使用了我编写的简单数组适配器,并且一切正常,请尝试用我的适配器替换您的适配器,并让我知道,谢谢。

class ItemsAdapter extends ArrayAdapter<String> {

    public ItemsAdapter(Context context, List<String> list) {
        super(context, R.layout.griditems, list);
    }

    @Override
    public View getView(final int position, View row, final ViewGroup parent) {
        final String item = getItem(position);

        ItemWrapper wrapper = null;
        if (row == null) {
            row = getLayoutInflater().inflate(R.layout.griditems, parent, false);
            wrapper = new ItemWrapper(row);

            row.setTag(wrapper);
        } else {
            wrapper = (ItemWrapper) row.getTag();
        }
        wrapper.refreshData(item);

        return row;
    }

}    

static class ItemWrapper {

    TextView text;
    ImageView img;

    public ItemWrapper(View row) {
        text = (TextView) row.findViewById(R.id.grid_item_text);
        img = (ImageView) row.findViewById(R.id.grid_item_image);
    }

    public void refreshData(String item) {
        img.setImageResource(R.drawable.image_1);
        text.setText(item);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM