Android单页日历视图

时间:2015-01-16 10:59:19

标签: android layout android-calendar

我正在尝试开发一个日历应用程序,它在一个活动中显示一年中的所有月份。我遇到了麻烦。 This是我想要的观点。我迷路了:/

ListViewAdapter:

package com.example.calendar15;


import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class CalendarListAdapter extends BaseAdapter {
    private Context mContext;
    String[] months; 
    public String[] numbers;


    // Constructor
    public CalendarListAdapter(Context c,String[] months) {
        mContext = c;
        this.months=months;
    }

    public int getCount() {
        return months.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.grid, parent, false);
        }
        // Lookup view for data population
        TextView tvTitle = (TextView) convertView.findViewById(R.id.date);
        Log.d("months[position]",""+months[position]);
        tvTitle.setText(months[position]);
        /**/
        String[] numbers = { "1", "2", "3", "4", "5", "6", "7","8","9","10","11","12"};


        /*TextView tvDate1 = (TextView) convertView.findViewById(R.id.text1);
        Log.d("numbers[position]",""+numbers[position]);
        tvDate1.setText(numbers[position]);*/

        GridView gridview = (GridView)convertView.findViewById(R.id.gridview);
        CalendarGridAdapter calendarGridAdapter = new CalendarGridAdapter(mContext, numbers);
        gridview.setAdapter(calendarGridAdapter);
        return convertView;
    }

}

GridViewAdapter:

package com.example.calendar15;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CalendarGridAdapter extends BaseAdapter {

    Context mContext;
    String[] number;

    CalendarGridAdapter(Context context, String[] numbers) {
        this.mContext = context;
        this.number = numbers;
        Log.d("number.length",""+number.length);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return number.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return number[position];
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

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

        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.calendar_item, parent, false);
        }
        // Lookup view for data population
        TextView tvTitle = (TextView) convertView.findViewById(R.id.date);
        Log.d("number[position]",""+number[position]);
        tvTitle.setText(number[position]);

        return convertView;
    }

}

Grid.xml

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:orientation="vertical">


    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#727272"
        android:textSize="16sp"
        android:gravity="center"
        android:text="date"
        android:textStyle="normal" />

     <GridView
        android:id="@+id/gridview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:listSelector="@android:color/transparent"
        android:numColumns="7"
        android:stretchMode="columnWidth" />

</GridLayout>

calendar_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Datelayer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#fff"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="2dip" >

    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="date"
        android:textColor="#727272"
        android:textSize="16sp"
        android:textStyle="normal" >
    </TextView>



</LinearLayout>

1 个答案:

答案 0 :(得分:0)

<强>解决方案

我从wrap_content更改了Gridview的高度并将其设置为大约200dp - 要膨胀的元素的高度乘以行的总数。 (您可以根据您的要求动态设置或在xml中设置它)。我希望这对其他人也有帮助