57.android 简单的自定义DatePicker颜色,隐藏头部

//有些属性用不了的话,就把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"
    }

 

/第一步 我的布局:




    
    
    
    
    

 

//第二步 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。

你可能感兴趣的:(android原生控件)