57.android 簡單的自定義DatePicker顏色,隱藏頭部
阿新 • • 發佈:2019-01-22
//有些屬性用不了的話,就把api改到21以上就行了。
//例如:
android { compileSdkVersion 26 defaultConfig { applicationId "com.example.hasee.a828huadong" minSdkVersion 21 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" }
/第一步 我的佈局:
<?xml version="1.0" encoding="utf-8"?> <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" android:orientation="vertical" tools:context="com.example.hasee.a828huadong.Main2Activity"> <!--firstDayOfWeek是設定第一個是星期幾,我寫的2,是星期一第一個--> <!-- android:headerBackground="#808080"是設定頭佈局的顏色,不過我隱藏頭佈局了--> <!--android:headerBackground="#808080"--> <DatePicker android:layout_gravity="center" android:theme="@style/AppTheme2" android:firstDayOfWeek="2" android:id="@+id/datePicker" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
//第二步 Activity裡隱藏頭佈局,並點選哪個日期,哪個就吐司資訊
public class Main2Activity extends AppCompatActivity { private DatePicker datePicker; Calendar c; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); initView(); } private void initView() { datePicker = (DatePicker) findViewById(R.id.datePicker); //這句隱藏頭佈局 ((LinearLayout) ((ViewGroup)datePicker.getChildAt(0)).getChildAt(0)).setVisibility(View.GONE); c=Calendar.getInstance(); //將日曆初始化為當前系統時間,並設定其事件監聽 datePicker.init(c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub //當前日期更改時,在這裡設定 int monthOfYear1 = monthOfYear+1; c.set(year,monthOfYear,dayOfMonth); ToastUtil.show(Main2Activity.this,year+","+monthOfYear1+","+dayOfMonth+""); } }); } }
//第三步 自定義一個style。
<style name="AppTheme2" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<!--這個綠色是選中時的背景顏色,和當前日期的顏色-->
<item name="colorAccent">@color/Green</item>
<!--設定字型顏色-->
<item name="android:textColorPrimary">@android:color/holo_blue_dark</item>
</style>