使用 3 行填充整个屏幕的图像视图创建 gridview [英] Creating gridview with 3 rows of imageviews that fill the whole screen

查看:16
本文介绍了使用 3 行填充整个屏幕的图像视图创建 gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在到处寻找类似的解决方案,但似乎没有一个适合我.

I have been searching everywhere for similar solutions but none seem to work for me.

在我的第一个屏幕上,我将有一个由 1 列和 3 行垂直组成的网格视图.每行将在文本视图顶部有一个图像视图和一个部分透明的文本视图.图像视图可以很好地跨越屏幕宽度.我唯一的问题是 3 个图像视图没有垂直跨越全屏,行之间有空间,尽管我尝试了很多方法来解决这个问题.我将发布我的 xml 文件和代码:

On my first screen I will have a gridview which consists of 1 column and 3 rows vertically. Each row will have an imageview and a partially transparent textview on top of the text view. The imageviews span the screen width fine. My only problem is that the 3 image views do not span the full screen vertically, there is space inbetween rows, although I have tried many methods to fix this. I will post up my xml files and code:

row_layout.xml - 这是网格视图项

row_layout.xml - this is the gridview items

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ImageView
    android:id="@+id/item_image"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    android:layout_width="fill_parent"
    android:layout_height="180dp"
    android:src="@drawable/season1">
</ImageView>

<TextView
    android:id="@+id/item_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/item_image"
    android:layout_alignTop="@+id/item_image"
    android:layout_alignRight="@+id/item_image"
    android:layout_alignBottom="@+id/item_image"
    android:gravity="bottom|left"
    android:textSize="30sp"
    android:textAlignment="center"
    android:lines="1"
    android:layout_marginTop="145dp"
    android:background="#99000000">


</TextView>

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >

<GridView
    android:id="@+id/gridView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="0dp"
    android:padding="0dp"
    android:gravity="center"
    android:numColumns="1"
    android:fitsSystemWindows="true"
    android:stretchMode="columnWidth" >
</GridView>

我的自定义适配器:

public class CustomGridViewAdapter extends ArrayAdapter<Item> {
Context context;
int layoutResourceId;
ArrayList<Item> data = new ArrayList<Item>();

public CustomGridViewAdapter(Context context, int layoutResourceId, ArrayList<Item> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    RecordHolder holder = null;

    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        row.setMinimumHeight(MainActivity.height1/3);

        holder = new RecordHolder();
        holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
        holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
        row.setTag(holder);
    } else {
        holder = (RecordHolder) row.getTag();
    }

    Item item = data.get(position);
    holder.txtTitle.setText(item.getTitle());
    holder.imageItem.setImageBitmap(item.getImage());// my image
    return row;

}

static class RecordHolder {
    TextView txtTitle;
    ImageView imageItem;

}

}

主要活动:

public class MainActivity extends ActionBarActivity {

GridView gridView;
ArrayList<Item> gridArray = new ArrayList<Item>();
CustomGridViewAdapter customGridAdapter;
public static int height1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);

    ActionBar ab = getActionBar();
    ab.setDisplayShowTitleEnabled(false);
    ab.setDisplayShowHomeEnabled(false);

    Resources res = this.getResources();

    Bitmap bMap = BitmapFactory.decodeResource(res, R.drawable.actionbar);
    BitmapDrawable actionBarBackground = new BitmapDrawable(res, bMap);
    ab.setBackgroundDrawable(actionBarBackground);

    //set grid view item

    Bitmap season1Icon = BitmapFactory.decodeResource(res, R.drawable.season1);
    Bitmap season2Icon = BitmapFactory.decodeResource(res, R.drawable.season2);
    Bitmap season3Icon = BitmapFactory.decodeResource(res, R.drawable.season3);


    gridArray.add(new Item(season1Icon,"Season 1"));
    gridArray.add(new Item(season2Icon,"Season 2"));
    gridArray.add(new Item(season3Icon,"Season 3"));

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    height1 = metrics.heightPixels;

    gridView = (GridView) findViewById(R.id.gridView1);
    customGridAdapter = new CustomGridViewAdapter(this, R.layout.row_grid, gridArray);

    gridView.setAdapter(customGridAdapter);

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {
            Intent a = new Intent(MainActivity.this, House.class);
            a.putExtra("Position", position);
            startActivity(a);
        }

    });
}

}

非常感谢任何帮助,我已经花了几个小时试图解决这个问题!

Any help will be greatly appreciated, I've spent hours trying to fix this!

推荐答案

在知道屏幕高度后,动态设置 imageview 的布局参数.

Dynamically set the layout params of your imageview, once you know the height of the screen.

if (row == null) {
    LayoutInflater inflater = ((Activity) context).getLayoutInflater();
    row = inflater.inflate(layoutResourceId, parent, false);
    //row.setMinimumHeight(MainActivity.height1/3);  //don't need this, since wrap content will make the row height match your image view's

    holder = new RecordHolder();
    holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
    holder.imageItem = (ImageView) row.findViewById(R.id.item_image);

    holder.imageItem.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, MainActivity.height1/3);
    row.setTag(holder);
} else {
    holder = (RecordHolder) row.getTag();
}

这篇关于使用 3 行填充整个屏幕的图像视图创建 gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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