简体   繁体   中英

CalendarView height to wrap current month

Well the question says it all, I am using a CalendarView in my layout for a simple month view where a user can select a date but for some reason the CalendarView is just taking up the whole screen, is there a way to make the CalendarView only display one month at a time instead of taking up the whole screen ? thanks

xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

<CalendarView
    android:id="@+id/calendar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</CalendarView>

</LinearLayout>

java :

public class CalendarActivity extends AppCompatActivity {

     CalendarView calendarview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calendar_layout);

        calendarview = (CalendarView) findViewById(R.id.calendar);

}
}

It's not quite simple though. I did use a calenderview, I did it using this library on github

https://github.com/SundeepK/CompactCalendarView

Try using it, it's quite flexible and extendable!

If the user will pick up a date only, you can use the DatePickerFragment. It appears only one month since Lollipop.

Creating a DatePickerFragment

public static class DatePickerFragment extends DialogFragment
                        implements DatePickerDialog.OnDateSetListener {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    // Create a new instance of DatePickerDialog and return it
    return new DatePickerDialog(getActivity(), this, year, month, day);
}

public void onDateSet(DatePicker view, int year, int month, int day) {
    // Do something with the date chosen by the user
    }
}

showing the Fragmet

DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");

Hope this help, more info for Pickers here

Put it inside a ScrollView

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <CalendarView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/calendarView" />
</ScrollView>

that works for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM