簡體   English   中英

在導航抽屜中將選定的項目設為粗體,但在應用啟動時無法使第一個項目變為粗體

[英]Making selected item bold in navigation drawer, but can't get the first item to be bold when the app starts

因此,我試圖在導航抽屜中將所選項目的文本設為粗體(例如Google Play應用程序),但是在首次啟動該應用程序時我無法使第一個項目變為粗體。工作

private class DrawerItemClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            setNavDrawerItemNormal();
            TextView TV = (TextView) view.findViewById(R.id.text1);
            TV.setTypeface(null, Typeface.BOLD);
            selectItem(position);
        }
    }

    public void setNavDrawerItemNormal() {
        for (int i = 0; i < mDrawerList.getChildCount(); i++) {
            View view = mDrawerList.getChildAt(i);
            TextView TV = ((TextView) view.findViewById(R.id.text1));
            TV.setTypeface(null, Typeface.NORMAL);
        }
    }

我嘗試將其添加到onCreate()方法中,但是由於第二行它引發了錯誤

View view = mDrawerList.getChildAt(0);
TextView TV = (TextView) view.findViewById(R.id.text1);
TV.setTypeface(null, Typeface.BOLD);

LogCat說這是由java.lang.NullPointerException引起的。知道為什么會這樣嗎? 以及如何使其工作?

問題是因為mDrawerList.getChildAt(0)返回第一個可見子級,而不是列表中的第一項。

您可能想在適配器類的getView()方法中實現結果:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ...

    if(position == 0) {
        // set bold text here
    }

    ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM