我想制作自定义日历,为此,我使用GridView
(每月12个单元格),每月嵌入GridView
s(显示几个月的日期)。我做了非常简单的布局和代码,只是为了检查它的外观。问题是我得到12个单元格,但每个单元格只显示1行内部网格(月份)。因此它显示为" 0 1 2 3 4 5 6
"对于每个12(月)的细胞。
首先,它更糟糕 - 它只显示单排外网格和单排内网格。经过一些实验,我解决了这个问题:问题在于我的主要活动有一些特殊类的根元素(这是由Android工作室自动生成的),我已将其更改为LinearLayout
并且它开始显示所有外部网格的细胞,但仍然只有一排内部。首先,我的内部布局包含在RelativeLayout
中,我将它们全部更改为LinearLayout
,希望这会解决它 - 但是没有任何变化。我也尝试了宽度和高度,没有运气,我不知道下一步该尝试什么。
以下是我的布局:
activity_main
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/calendar_year"></include>
</LinearLayout>
calendar_year(外部网格)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<GridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/grid_year"
android:numColumns="3"
android:gravity="center"
android:stretchMode="columnWidth" />
</LinearLayout>
calendar_month(内部网格)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<GridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/grid_month"
android:numColumns="7"
android:gravity="center"
android:stretchMode="columnWidth" />
</LinearLayout>
calendar_day(单元格内容)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/text_day"
android:textColor="@android:color/black" />
</LinearLayout>
MainActivity.java的onCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
((GridView) this.findViewById(R.id.grid_year)).setAdapter(new YearGridAdapter(getApplicationContext()));
}
外网格适配器:
public class YearGridAdapter extends BaseAdapter {
public YearGridAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return 12;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.calendar_month, parent, false);
}
GridView monthView = (GridView) convertView.findViewById(R.id.grid_month);
monthView.setAdapter(new MonthGridAdapter(context));
return convertView;
}
private Context context;
}
内部网格的适配器:
public class MonthGridAdapter extends BaseAdapter {
public MonthGridAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return 30;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.calendar_day, parent, false);
}
((TextView) convertView.findViewById(R.id.text_day)).setText(Integer.toString(position));
return convertView;
}
private Context context;
}
答案 0 :(得分:1)
这可能是由GridView
内GridView
引起的。由于两种布局都是可滚动的,因此会导致很多问题。你可以看到这个link Arun Antoney的答案可以解决你的问题