如何在 CalendarView 中只显示日期栏?

How to show only date bar in a CalendarView?

我只想显示这部分 CalendarView 没有日历

这样只有日期和箭头可见。


我找不到我是否应该自己实施它,或者是否有一些解决方法或其他一些 View 可以帮助我实现这种外观。

提前致谢。

您必须创建自己的自定义日期选择器。也许this tutorial可以帮到你。

您可以通过创建自定义当前日期视图来获取它,如下所示,

-------- activity_main.xml --------

<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="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="@dimen/margin_60dp"
    android:orientation="horizontal"
    android:paddingTop="@dimen/margin_10dp"
    android:paddingBottom="@dimen/margin_10dp">
    <ImageButton
        android:id="@+id/backBtn"
        android:layout_width="@dimen/margin_40dp"
        android:layout_height="@dimen/margin_40dp"
        android:layout_margin="@dimen/margin_10dp"
        android:layout_gravity="center"
        android:background="@drawable/ic_action_back" />
    <TextView
        android:id="@+id/current_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Current Date"
        android:textSize="18sp"
        android:textStyle="bold" />
    <ImageButton
        android:id="@+id/nextBtn"
        android:layout_width="@dimen/margin_40dp"
        android:layout_gravity="center"
        android:layout_height="@dimen/margin_40dp"
        android:layout_margin="@dimen/margin_10dp"
        android:background="@drawable/ic_action_next" />
</LinearLayout>

------ MainActivity.java ------

public class MainActivity extends AppCompatActivity {
ImageButton nextBtn, backBtn;
TextView CurrentDate;
private static final int MAX_CALENDAR_DAYS = 42;
Calendar calendar = Calendar.getInstance(Locale.ENGLISH);
List<Date> dates = new ArrayList<>();
SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM YYYY", Locale.ENGLISH);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    backBtn = (ImageButton) findViewById(R.id.backBtn);
    nextBtn = (ImageButton) findViewById(R.id.nextBtn);
    CurrentDate = (TextView) findViewById(R.id.current_date);
    backBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            calendar.add(Calendar.MONTH, -1);
            SetUpCalendar();
        }
    });
    nextBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            calendar.add(Calendar.MONTH, 1);
            SetUpCalendar();
        }
    });
}
private void SetUpCalendar() {
    String currentDate = dateFormat.format(calendar.getTime());
    CurrentDate.setText(currentDate);
    dates.clear();
    Calendar monthCalendar = (Calendar) calendar.clone();
    monthCalendar.set(Calendar.DAY_OF_MONTH, 1);
    int FirstDayofMonth = monthCalendar.get(Calendar.DAY_OF_WEEK) - 1;
    monthCalendar.add(Calendar.DAY_OF_MONTH, -FirstDayofMonth);
    while (dates.size() < MAX_CALENDAR_DAYS) {
        dates.add(monthCalendar.getTime());
        monthCalendar.add(Calendar.DAY_OF_MONTH, 1);
    }
}

}

这篇文章可能对您有所帮助!

我找不到如何使用 CalendarView 制作这样的内联日历,所以我想出了自己的实现:
------inline_calendar.xml------

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:id="@+id/calendar_prev_button"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginEnd="40dp"
        android:layout_marginRight="40dp"
        android:background="@drawable/ic_baseline_chevron_left_24px" />

    <TextView
        android:id="@+id/display_date"
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:gravity="center"
        android:text="Feb 2019"
        android:textAppearance="@android:style/TextAppearance.Large"
        android:textColor="#222222"
        android:textSize="30sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/calendar_next_button"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginStart="40dp"
        android:layout_marginLeft="40dp"
        android:background="@drawable/ic_baseline_chevron_right_24px" />
</LinearLayout>

--------InlineCalendar.java--------

public class InlineCalendar extends LinearLayout {
    private Button btnPrev;
    private Button btnNext;
    private TextView tvDate;
    private Calendar calendar = Calendar.getInstance();
    private MyOnClickListener myOnClickListener;
    private static final String TAG = "InlineCalendar";

    public InlineCalendar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initControl(context);
        initInterface();
    }

    public void setListener(MyOnClickListener myOnClickListener) {
        this.myOnClickListener = myOnClickListener;
    }

    public Calendar getCalendar() {
        return calendar;
    }

    private void assignUiElements() {
        // layout is inflated, assign local variables to components
        btnPrev = findViewById(R.id.calendar_prev_button);
        btnNext = findViewById(R.id.calendar_next_button);
        tvDate = findViewById(R.id.display_date);
    }

    private void initInterface() {
        myOnClickListener = new MyOnClickListener() {
            @Override
            public void onClickListener() {
            }
        };
    }

    private void assignClickHandlers() {
        assignOnClickListener(btnPrev, -1);
        assignOnClickListener(btnNext, 1);
        btnNext.setVisibility(View.INVISIBLE);
    }

    private void assignOnClickListener(final Button btn, final int addMonth) {
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                updateCalendar(addMonth);
                myOnClickListener.onClickListener();
                boolean condition = (Calendar.getInstance().get(Calendar.MONTH) == calendar.get(Calendar.MONTH) &&
                        Calendar.getInstance().get(Calendar.YEAR) == calendar.get(Calendar.YEAR));
                btnNext.setVisibility(condition ? View.INVISIBLE : View.VISIBLE);
            }
        });
    }

    private void initControl(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.inline_calendar, this);

        assignUiElements();
        assignClickHandlers();

        updateCalendar(0);
    }

    public void updateCalendar(int addMonth) {
        calendar.add(Calendar.MONTH, addMonth);
        tvDate.setText(calendarToString());
    }

    private String calendarToString() {
        SimpleDateFormat date_format = new SimpleDateFormat("MMM yyyy", Locale.ENGLISH);
        return date_format.format(calendar.getTime());
    }

    public interface MyOnClickListener {
        void onClickListener();
    }
}

要使用 InlineCalendar,只需在您的 Activity's XML 中包含以下代码:

<com.example.Activity.InlineCalendar
        android:id="@+id/inlineCalendar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

此外,您可以使用 setListener(MyOnClickListener myOnClickListener) 方法将自定义侦听器分配给上一个和下一个按钮,就像在 CalendarView

中一样