我正在尝试制作一个自定义的Calendar控件来填充屏幕。日历为7天乘6周(静态)。 我正在使用GridLayout(android.support.V7.widget.GridLayout)作为屏幕上日期的布局。
使用单独的布局文件将网格的内容加载。
但是,网格中各项的布局似乎都放在了左上角。 LayoutGrid是父级的大小,但是所有项目都尽可能小(即使在项目上具有编码的大小)。
我正在寻找一种布局来填满整个屏幕。 我知道我可以使用嵌套的LinearLayout布局来做到这一点,但这违背了GridLayout的目的。
任何帮助将不胜感激。
P.S。白天的列表视图还会导致意外的布局,每天都是屏幕的整个宽度。
GridLayout布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0FC"
android:paddingTop="?attr/actionBarSize">
<android.support.v7.widget.GridLayout
android:id="@+id/grdlayCalendarMonth"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FC0"
app:alignmentMode="alignBounds"
app:columnCount="7"
app:rowCount="6"
app:useDefaultMargins="true"
/>
</LinearLayout>
日期布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="250dp"
android:layout_height="250dp"
android:background="#4444ff"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
app:layout_gravity="fill_vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardCornerRadius="3dp"
app:cardElevation="3dp"
app:cardUseCompatPadding="true"
android:layout_margin="0dp"
android:paddingVertical="3dp"
app:cardBackgroundColor="@color/cardTextGreen">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/layoutCalendarDay"
android:background="#fc03e8">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lblCalendarDayDate"/>
<!--<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/lvwCalendarDay"/>-->
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
向GridLayout添加日期
Android.Support.V7.Widget.GridLayout monthGrid = FindViewById<Android.Support.V7.Widget.GridLayout>(Resource.Id.grdlayCalendarMonth);
//Find the first day of the month.
DateTime firstDayOfMonth = DateTime.Now.GetFirstDayOfMonth();
DayOfWeek firstDayOfWeek = firstDayOfMonth.DayOfWeek;
DateTime firstDayOfDisplayCalendar = firstDayOfMonth.AddDays(-(int)firstDayOfWeek);
for (int r = 0; r < 6; r++) //ROW
{
for (int c = 0; c < 7; c++) //COLUMN
{
Android.Support.V7.Widget.GridLayout.LayoutParams param = new Android.Support.V7.Widget.GridLayout.LayoutParams(
Android.Support.V7.Widget.GridLayout.InvokeSpec(r, 1), Android.Support.V7.Widget.GridLayout.InvokeSpec(c, 1));
View dayView = View.Inflate(this, Resource.Layout.calendar_month_dayview, null);
dayView.LayoutParameters = param;
TextView dayListDate = dayView.FindViewById<TextView>(Resource.Id.lblCalendarDayDate);
dayView.Tag = firstDayOfDisplayCalendar.Ticks;
dayListDate.Text = firstDayOfDisplayCalendar.ToString("dd").Ordinalize();
firstDayOfDisplayCalendar = firstDayOfDisplayCalendar.AddDays(1);
monthGrid.AddView(dayView, param);
}
}
更新: 虽然下面的答案可以显示。当任何额外的控件添加到网格日布局时,列和行的缩放比例都会更改,并且所有这些都将再次不同步。我将转到嵌套的LinearLayout结构以节省时间。
答案 0 :(得分:1)
根据描述,您希望拆分GridLayout项的屏幕行高和列宽,如果是,则可以查看以下代码来修改代码。
您可以使用
GridLayout.InvokeSpec(r, GridLayout.Fill, 1f)
平均当前可用空间。
for (int r=0;r<5;r++)
{
for(int c=0;c<7;c++)
{
var row = GridLayout.InvokeSpec(r, GridLayout.Fill, 1f);
var col = GridLayout.InvokeSpec(c, GridLayout.Fill,1f);
View layout1 = View.Inflate(this, Resource.Layout.layout1, null);
TextView textview = layout1.FindViewById<TextView>(Resource.Id.textView1);
textview.Text = "test!";
layout1.LayoutParameters = new GridLayout.LayoutParams(row, col);
monthGrid.AddView(layout1);
}
}
如果我的回复有帮助,请记住将我的回复标记为答案,谢谢。