android DatePicker 隱藏頭部

android5.0及以上的版本中,DatePicker 在 calendar 模式下,自帶了一個(gè)快速切換年份和日期的頭部:


帶頭部的DatePicker

隱藏這個(gè)頭部的原理就是找到相應(yīng)的 View 并設(shè)置 Visibility 為 Gone.
在 DatePicker 源碼中,如果 mode 使用的是 Calendar 則會(huì)調(diào)用 createCalendarUIDelegate 方法。

switch (mode) {    
    case MODE_CALENDAR:        
        mDelegate = createCalendarUIDelegate(context, attrs, defStyleAttr, defStyleRes);        
        break;    
    case MODE_SPINNER:    
    default:        
        mDelegate = createSpinnerUIDelegate(context, attrs, defStyleAttr, defStyleRes);        
        break;
}

createCalendarUIDelegate 則會(huì)返回一個(gè) DatePickerSpinnerDelegate

private DatePickerDelegate createCalendarUIDelegate(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {    
    return new DatePickerCalendarDelegate(this, context, attrs, defStyleAttr, defStyleRes);
}

其中 DatePickerCalendarDelegate 使用的布局文件為 R.layout.date_picker_material

final int layoutResourceId = a.getResourceId(R.styleable.DatePicker_internalLayout, R.layout.date_picker_material);
// Set up and attach container.
mContainer = (ViewGroup) inflater.inflate(layoutResourceId, mDelegator, false);
mDelegator.addView(mContainer);

R.layout.date_picker_material 的內(nèi)容為

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"              
    android:layout_width="wrap_content"              
    android:layout_height="wrap_content"              
    android:orientation="vertical">    
    <include        
        layout="@layout/date_picker_header_material"        
        android:layout_width="match_parent"        
        android:layout_height="wrap_content" />    
    <include        
        layout="@layout/date_picker_view_animator_material"        
        android:layout_width="match_parent"        
        android:layout_height="0dp"        
        android:layout_weight="1" />
</LinearLayout>

第一個(gè) include 的布局就是頭部了,接下來要做的就是找到這個(gè) View 然后隱藏掉。

ViewGroup rootView = (ViewGroup) datePicker.getChildAt(0);
if (rootView == null) {    
    return;
}
View headerView = rootView.getChildAt(0);
if (headerView == null) {    
    return;
}
headerView.setVisibility(View.GONE);

考慮到安全性,為了保證隱藏掉的 View 就是想要隱藏的頭部,可以加上 id 的判斷。

  • 5.0中,頭部根布局的 id 為 day_picker_selector_layout
  • 6.0及以上,頭部根布局的 id 為 date_picker_header

設(shè)置為 gone 之后還要?jiǎng)討B(tài)的調(diào)整一下布局,所以最后的方法為:

private void hideDatePickerHeader(DatePicker datePicker) {    
    ViewGroup rootView = (ViewGroup) datePicker.getChildAt(0);    
    if (rootView == null) {        
        return;    
    }    
    View headerView = rootView .getChildAt(0);    
    if (headerView == null) {       
        return;    
    }    
    //5.0+   
    int headerId = context.getResources().getIdentifier("day_picker_selector_layout", "id", "android");    
    if (headerId == headerView.getId()) {        
        headerView.setVisibility(View.GONE);        
        
        ViewGroup.LayoutParams layoutParamsRoot = rootView.getLayoutParams();        
        layoutParamsRoot.width = ViewGroup.LayoutParams.WRAP_CONTENT;  
        rootView.setLayoutParams(layoutParamsRoot);        
        
        ViewGroup animator = (ViewGroup) rootView.getChildAt(1);        
        ViewGroup.LayoutParams layoutParamsAnimator = animator.getLayoutParams();        
        layoutParamsAnimator.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        animator.setLayoutParams(layoutParamsAnimator);        

        View child = animator.getChildAt(0);        
        ViewGroup.LayoutParams layoutParamsChild = child.getLayoutParams();        
        layoutParamsChild.width = ViewGroup.LayoutParams.WRAP_CONTENT;        
        child.setLayoutParams(layoutParamsChild );       
        return;   
     }    
    //6.0+   
    headerId = context.getResources().getIdentifier("date_picker_header", "id", "android");    
    if (headerId == headerView.getId()) {        
        headerView.setVisibility(View.GONE);    
    }
}

最終效果:


隱藏頭部的DatePicker
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容