嗯,我的問題很簡單。將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"
,但仍然沒有。
我不知道這裏有什麼問題。也許邏輯是好的,但滾動條的屬性不是我所需要的。對不起,如果它有點混亂,但我想告訴你我嘗試過的東西。
再次感謝您。
我想確認一件事:你想讓網格填滿屏幕,那麼你需要滾動嗎?如果你願意,你會有一個情況,你會有更多的項目比屏幕尺寸可以填補? – Parnit
不是。這是日曆的主要格子...總是6x7。所以,我不需要任何滾動。 – Kashmir