2014-03-25 55 views
0

嗯,我的问题很简单。将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,这是我想要的行数......但我不确定这是否正确,既不知道如何做到这一点。

在此先感谢!

编辑:

东西我没有说:我有一个自定义适配器来填充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",但仍然没有。

我不知道这里有什么问题。也许逻辑是好的,但滚动条的属性不是我所需要的。对不起,如果它有点混乱,但我想告诉你我尝试过的东西。

再次感谢您。

+0

我想确认一件事:你想让网格填满屏幕,那么你需要滚动吗?如果你愿意,你会有一个情况,你会有更多的项目比屏幕尺寸可以填补? – Parnit

+0

不是。这是日历的主要格子...总是6x7。所以,我不需要任何滚动。 – Kashmir

回答

0

我会建议您使用的代码(如果身高看起来很好)

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

并禁用GridView的滚动。以下链接的代码显示了如何禁用滚动How to disable GridView scrolling in Android?

+0

它工作了!但我想我可能不得不改变别的东西。我的想法之一是滚动左/右来改变月份。如果我继续使用这个功能,我认为我不能这样做,我可以吗?无论如何,这工作正常,非常感谢。 – Kashmir

0
<?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" > 


    <GridView 
     android:id="@+id/gridView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:numColumns="3" > 
    </GridView> 

</LinearLayout> 
+0

谢谢你的回复!但它没有奏效。 =(我刚刚在帖子中添加了更多信息,也许它不是很清楚, – Kashmir

相关问题