【问题标题】:How to change Month text color in CalendarView如何在 CalendarView 中更改月份文本颜色
【发布时间】:2019-01-24 09:44:45
【问题描述】:

我在我的项目中使用 CalendarView,我需要为它做一个特殊的设计。我可以更改 CalendarView 中所有事物的颜色,但不能更改日历顶部月份的颜色。我尝试了 xml 中的所有属性。如何更改月份铭文和箭头的颜色?

<CalendarView
        android:id="@+id/calendarID"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="Heh"
        android:maxDate="31/12/2018"
        android:minDate="09/01/2012"
        android:showWeekNumber="false"
        android:focusedMonthDateColor="@android:style/TextAppearance.Small"
        android:background="#004366"
        android:unfocusedMonthDateColor="@android:style/TextAppearance.Small"
        android:dateTextAppearance="@android:style/TextAppearance.Small"
        android:weekDayTextAppearance="@android:style/TextAppearance.Small"
        android:weekSeparatorLineColor="@android:style/TextAppearance.Small"
        android:selectedWeekBackgroundColor="@android:style/TextAppearance.Small"
        />

 LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
            (Context.LAYOUT_INFLATER_SERVICE);

    LinearLayout ll= (LinearLayout)inflater.inflate(R.layout.calendar_moi, null, false);

    CalendarView cv = (CalendarView) ll.getChildAt(0);
    Long maxData = System.currentTimeMillis();
    cv.setMaxDate(maxData);

    cv.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

        @Override
        public void onSelectedDayChange(CalendarView view, int year, int month,
                                        int dayOfMonth) {


        }
    });

    new AlertDialog.Builder(ItemClassActivity.this)
            .setTitle("")
            .setMessage("")
            .setView(ll)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            }
    ).show();

【问题讨论】:

标签: android android-layout android-view android-styles calendarview


【解决方案1】:

styles.xml中定义了以下内容:

<style name="CustomCalendarMonth" parent="AppTheme">
    <item name="android:textColorPrimary">@color/orange</item>
</style>

<style name="CustomCalendarDay" parent="TextAppearance.MaterialComponents.Caption">
    <item name="android:textColor">@color/green</item>
</style>

<style name="CustomCalendarWeek" parent="TextAppearance.MaterialComponents.Caption">
    <item name="android:textColor">@color/purple</item>
</style>

然后在xml中:

<CalendarView
    android:theme="@style/CustomCalendarMonth"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:dateTextAppearance="@style/CustomCalendarDay"
    android:weekDayTextAppearance="@style/CustomCalendarWeek" />

输出如下:

【讨论】:

    【解决方案2】:

    检查此代码@Valentin。可能会对您有所帮助。您可以通过单击侧面按钮更改月份。

    我用过这个库:

    implementation 'com.github.sundeepk:compact-calendar-view:1.9.1'
    

    Calendar.xml

    xmlns:app="http://schemas.android.com/apk/res-auto"
    
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout_detail"
    android:layout_width="match_parent"
    
    android:layout_height="match_parent"
    
    android:orientation="vertical">
    
    <android.support.v7.widget.Toolbar
    
        android:id="@+id/toolbar"
    
        android:layout_width="match_parent"
    
        android:layout_height="?attr/actionBarSize"
    
        android:background="@color/colorPrimary"
    
        android:theme="@style/AppTheme">
    
        <ImageView
    
            android:id="@+id/image_back"
    
            android:layout_width="30dp"
    
            android:layout_height="20dp"
    
            android:layout_marginLeft="20dp"
    
            android:src="@drawable/ic_arrow_back_black_24dp" />
    
        <TextView
    
            android:layout_width="wrap_content"
    
            android:layout_height="wrap_content"
    
            android:layout_marginLeft="20dp"
    
            android:text="Calendar"
    
            android:textColor="@android:color/white"
    
            android:textSize="17dp"
    
            android:textStyle="bold" />
    
        <TextView
    
            android:id="@+id/text_today"
    
            android:layout_width="wrap_content"
    
            android:layout_height="wrap_content"
    
            android:layout_gravity="center|right"
    
            android:padding="10dp"
    
            android:text="Today"
    
            android:textColor="@android:color/white"
    
            android:textSize="15dp" />
    </android.support.v7.widget.Toolbar>
    
    <LinearLayout
    
        android:layout_width="match_parent"
    
        android:layout_height="400dp"
    
        android:layout_margin="10dp"
    
    
        android:gravity="center"
    
        android:orientation="vertical">
    
    
        <LinearLayout
    
            android:layout_width="match_parent"
    
            android:layout_height="60dp"
    
            android:gravity="center"
    
            android:orientation="horizontal">
    
            <LinearLayout
    
                android:id="@+id/layout_right"
    
                android:layout_width="40dp"
    
                android:layout_height="match_parent"
    
                android:gravity="center">
    
                <ImageView
    
                    android:layout_width="20dp"
    
                    android:layout_height="20dp"
    
                    android:background="@mipmap/ic_launcher" />
            </LinearLayout>
    
            <LinearLayout
    
                android:layout_width="match_parent"
    
                android:layout_height="match_parent"
    
                android:layout_weight="1"
    
                android:gravity="center">
    
                <TextView
    
                    android:id="@+id/text"
    
                    android:layout_width="wrap_content"
    
                    android:layout_height="wrap_content"
    
                    android:layout_gravity="center"
    
                    android:text="Date"
    
                    android:textColor="@color/red_shade_dashboard"
    
                    android:textSize="17dp"
    
                    android:textStyle="bold" />
            </LinearLayout>
    
            <LinearLayout
    
                android:id="@+id/layout_left"
    
                android:layout_width="40dp"
    
                android:layout_height="match_parent"
    
                android:gravity="center">
    
                <ImageView
    
                    android:layout_width="20dp"
    
                    android:layout_height="20dp"
    
                    android:background="@mipmap/ic_launcher" />
            </LinearLayout>
        </LinearLayout>
    
        <View
    
            android:layout_width="match_parent"
    
            android:layout_height="1dp"
    
            android:background="@color/red_shade_dashboard" />
    
        <LinearLayout
    
            android:layout_width="match_parent"
    
            android:layout_height="match_parent"
    
            android:layout_weight="0.2">
    
            <com.github.sundeepk.compactcalendarview.CompactCalendarView
    
                android:id="@+id/compactcalendar_view"
    
                android:layout_width="match_parent"
    
                android:layout_height="320dp"
    
                android:layout_margin="5dp"
    
                app:compactCalendarBackgroundColor="@android:color/white"
    
                app:compactCalendarCurrentDayBackgroundColor="@color/red_shade_dashboard"
                app:compactCalendarCurrentSelectedDayBackgroundColor="@color/red_shade_dashboard"
                app:compactCalendarMultiEventIndicatorColor="@color/red_shade_dashboard"
                app:compactCalendarTargetHeight="360dp"
    
                app:compactCalendarTextColor="@android:color/black"
    
                app:compactCalendarTextSize="15dp" />
        </LinearLayout>
    </LinearLayout>
    

    CalendarActivity.java

    public class CalenderViewActivity extends AppCompatActivity {
    
    CompactCalendarView compactCalendarView;
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-yyyy", Locale.getDefault());
    private SimpleDateFormat DateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    SimpleDateFormat sdf;
    TextView tx_date, tx_today;
    LinearLayout ly_detail;
    LinearLayout ly_left, ly_right;
    Calendar myCalendar;
    ImageView im_back;
    Date c;
    SimpleDateFormat df;
    String formattedDate;
    String[] dates = new String[0];
    RecyclerView recyclerView;
    TextView tx_item;
    
    String[] day={"10","20","21","25","27"};
    String[] month={"10","10","11","11","12"};
    String[] year ={"2018","2018","2018","2018","2018"};
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calender_view);
    
        init();
        calendarlistener();
        Setdate();
    
    
        tx_date.setText(""+formattedDate);
    
    
        ly_right.setOnClickListener(new View.OnClickListener() {
            @Override
    
            public void onClick(View v) {
                compactCalendarView.showCalendarWithAnimation();
                compactCalendarView.showNextMonth();
            }
        });
    
        ly_left.setOnClickListener(new View.OnClickListener() {
            @Override
    
            public void onClick(View v) {
                compactCalendarView.showCalendarWithAnimation();
                compactCalendarView.showPreviousMonth();
            }
        });
    
        tx_today.setOnClickListener(new View.OnClickListener() {
            @Override
    
            public void onClick(View v) {
    
                Intent intent = new Intent(CalenderViewActivity.this, CalenderViewActivity.class);
                startActivity(intent);
                finish();
    
            }
        });
    
        im_back.setOnClickListener(new View.OnClickListener() {
            @Override
    
            public void onClick(View v) {
                finish();
            }
        });
    
    
    }
    
    //variable initialization
    
    public void init() {
        compactCalendarView = (CompactCalendarView) findViewById(R.id.compactcalendar_view);
        tx_date = (TextView) findViewById(R.id.text);
        ly_left = (LinearLayout) findViewById(R.id.layout_left);
        ly_right = (LinearLayout) findViewById(R.id.layout_right);
        im_back = (ImageView) findViewById(R.id.image_back);
        tx_today = (TextView) findViewById(R.id.text_today);
        ly_detail = (LinearLayout) findViewById(R.id.layout_detail);
    
    
    
    }
    
    
    //calendar method
    
    public void calendarlistener() {
        compactCalendarView.setListener(new CompactCalendarView.CompactCalendarViewListener() {
            @Override
    
            public void onDayClick(Date dateClicked) {
    
                if ( DateFormat.format(dateClicked).equals("2018-11-21")){
                    Toast.makeText(getApplicationContext(),DateFormat.format(dateClicked)+" This day your brother birth day ",Toast.LENGTH_LONG).show();
                }
                else {
                    Toast.makeText(getApplicationContext(),DateFormat.format(dateClicked)+" In This day no Events Available",Toast.LENGTH_LONG).show();
                }
    
            }
    
            @Override
    
            public void onMonthScroll(Date firstDayOfNewMonth) {
    
                compactCalendarView.removeAllEvents();
                Setdate();
                tx_date.setText(simpleDateFormat.format(firstDayOfNewMonth));
    
            }
        });
    }
    
    //get current date
    
    public void Setdate() {
    
    
        c = Calendar.getInstance().getTime();
        df = new SimpleDateFormat("yyyy-MM-dd");
        formattedDate = df.format(c);
    
        compactCalendarView.setUseThreeLetterAbbreviation(true);
    
        sdf = new SimpleDateFormat("MMMM yyyy");
    
    
        myCalendar = Calendar.getInstance();
    
        for (int j = 0; j < month.length; j++) {
    
            int mon = Integer.parseInt(month[j]);
            myCalendar.set(Calendar.YEAR, Integer.parseInt(year[j]));
            myCalendar.set(Calendar.MONTH, mon - 1);
            myCalendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day[j]));
    
            Event event = new Event(Color.RED, myCalendar.getTimeInMillis(), "test");
            compactCalendarView.addEvent(event);
        }
    }
    

    输出 - https://i.stack.imgur.com/kYUdl.png

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多