我只想显示CalendarView
的这一部分而不显示日历
,以便仅显示日期和箭头。
我找不到我应该自己实现它,还是有其他解决方法或其他View
可以帮助我实现此外观的方法。
谢谢。
答案 0 :(得分:2)
您可以通过创建如下所示的自定义“当前日期”视图来获得它,
------- 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);
}
}
}
这可能会帮助您!
答案 1 :(得分:1)
您必须创建自己的自定义日期选择器。也许this tutorial可以为您提供帮助。
答案 2 :(得分:0)
我找不到如何使用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
方法将自定义侦听器分配给上一个和下一个按钮