调整GridView单元格的高度以适应剩余空间 [英] Adapt a GridView cell's height to remaining space

查看:41
本文介绍了调整GridView单元格的高度以适应剩余空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,我的问题很简单.

我正在开始Android编程,并且正在编写一个简单的日历应用程序.

我的主要布局是这样的(RelativeLayout上方还有其他内容,但我认为这并不重要):

<RelativeLayout 
   android:id
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_below="@+id/header" >

   <GridView
        android:id="@+id/month_days"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="7"
        android:stretchMode="columnWidth"
        android:columnWidth="60dp"
        android:horizontalSpacing="1dp"
        android:verticalSpacing="1dp" >
    </GridView>
</RelativeLayout>

我希望GridView使用布局中的每个像素,包括高度和宽度.我可以调整de columnWidth属性来实现这一点,但是我不知道该如何使用高度.

我正在考虑获取RelativeLayout尺寸(特别是高度),然后将其除以6,这就是我想要的行数...但是我不确定这是否正确,也不知道如何执行此操作./p>

提前谢谢!

我没说什么:我有一个自定义适配器来填充GridView及其属性.这是代码.

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:background="@color/actual_month_day" >

<TextView
    android:id="@+id/day_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center">
</TextView>
</RelativeLayout>

例如,如果更改android:layout_height="65dp",它将使单元格变大,这与我想要的类似,但是它不能覆盖整个屏幕.如果我写一个更大的值,我需要滚动查看整个网格.

问题在于价值.我希望"65dp"是动态的,并根据屏幕上的可用空间进行更改.

如果有帮助,这是我的适配器中的getView().

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

    LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View v;
    TextView actualDay;

    if (convertView == null) {
        v = new View(c);
        v = inflater.inflate(R.layout.calendar_day_2, parent, false);
        actualDay = (TextView) v.findViewById(R.id.day_2);
        actualDay.setText(days[position]);

        if(!isActualMonth(position)) v.setBackgroundColor(c.getResources().getColor(R.color.not_actual_month_day));
    } else {
        v = (View) convertView;
    }

    return v;       
}

我认为我必须在getView()代码中计算布局的剩余空间,但是我不知道如何.

我尝试添加actualDay.setHeight(parent.getHeight()/6);,但是并没有太大帮助.单元格的高度还可以,但是它会向上滚动,然后一切都消失了.之后,我设置了android:scrollbars="none",但仍然没有设置.

我不知道这里出了什么问题.逻辑也许不错,但是滚动条的属性不是我所需要的.抱歉,如果有点麻烦,但是我想告诉您我尝试过的事情.

再次感谢您.

解决方案

我建议您使用代码(如果高度看起来不错)

actualDay.setHeight(parent.getHeight()/6);

并禁用gridview的滚动.以下链接的代码显示了如何禁用滚动如何在Android中禁用GridView滚动?.

Well, my question is quite simple.

I am beginning Android programming and I am programming a simple calendar app.

My main layout is like this (There's more above the RelativeLayout, but I think it's not important):

<RelativeLayout 
   android:id
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_below="@+id/header" >

   <GridView
        android:id="@+id/month_days"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="7"
        android:stretchMode="columnWidth"
        android:columnWidth="60dp"
        android:horizontalSpacing="1dp"
        android:verticalSpacing="1dp" >
    </GridView>
</RelativeLayout>

I want the GridView to use every single pixel in the Layout, in height and in width. I could adjust de columnWidth property to gain that, but I don't know how to do this with height.

I was thinking in getting the RelativeLayout dimensions (height particularly), and dividing it by 6, which is the number of rows I want... But I'm not sure if this is correct neither how to do this.

Thanks in advance!

Edit:

Something I did not say: I have a custom adapter to populate the GridView and its properties. Here's the code.

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:background="@color/actual_month_day" >

<TextView
    android:id="@+id/day_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center">
</TextView>
</RelativeLayout>

If I change android:layout_height="65dp", for example, it makes the cell bigger, which is something similar to what I want, but it does not cover the entire screen. If I write a bigger value I need to scroll to see the whole grid.

The problem is the value. I want the "65dp" to be dynamic and change depending on the free space in the screen.

If it helps, here's the getView() in my adapter.

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

    LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View v;
    TextView actualDay;

    if (convertView == null) {
        v = new View(c);
        v = inflater.inflate(R.layout.calendar_day_2, parent, false);
        actualDay = (TextView) v.findViewById(R.id.day_2);
        actualDay.setText(days[position]);

        if(!isActualMonth(position)) v.setBackgroundColor(c.getResources().getColor(R.color.not_actual_month_day));
    } else {
        v = (View) convertView;
    }

    return v;       
}

I think that I have to calculate the Layout's remaining space in the getView() code, but I don't know how.

I've tried adding actualDay.setHeight(parent.getHeight()/6); but it didn't help me too much. The cell's height is ok, but it scrolls up and then everything disappears. After that, I set android:scrollbars="none", but still nothing.

I do not know what's the problem here. Maybe the logic is fine, but the property of the scrollbar is not what I need. Sorry if it is all a bit messy, but I wanted to tell you the things I've tried.

Thank you again.

解决方案

I would recommend that you use your code (if the height looks well)

actualDay.setHeight(parent.getHeight()/6);

and disable the scrolling for the gridview. The following link has code that shows how to disable scrolling How to disable GridView scrolling in Android?.

这篇关于调整GridView单元格的高度以适应剩余空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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