【问题标题】:Android navigation drawer, change text/hover colorAndroid 导航抽屉,更改文本/悬停颜色
【发布时间】:2021-08-20 00:21:22
【问题描述】:

我有两个关于给 android studio 的导航抽屉模板的问题。

)

我想更改菜单的文本颜色(“notre histoire”等)和所选项目的悬停(这里是绿色,我想用其他颜色)。

如您所见,我设法更改了操作栏的背景颜色(此处为粉红色)并更改了菜单的背景(此处为蓝色)。

但在我的情况下,我没有找到如何更改文本颜色和所选项目的悬停。

我的限制是我不能触摸 xml 文件。我必须以编程方式进行。

这是我将菜单字符串提供给应用程序的方式:

String [] strTabMenu = new String[2];
strTabMenu[0] = "test1";
strTabMenu[1] = "test2";

mDrawerListView.setAdapter(new ArrayAdapter<String>(
                getActionBar().getThemedContext(),
                android.R.layout.simple_list_item_activated_1,
                android.R.id.text1,
                strTabMenu));

那么,我现在如何通过一些代码行来更改文本颜色和悬停颜色而不创建/更新一些 xml 文件?

谢谢 =)

【问题讨论】:

    标签: java android navigation-drawer


    【解决方案1】:

    您可以编写自己的列表适配器,而不是使用默认的 ArrayAdapter:

    public class DrawerListAdapter extends BaseAdapter{
    
    private Context context;
    private String[] mTitle;
    private int[] mIcon;
    private LayoutInflater inflater;
    
    public DrawerListAdapter(Context pContext, String[] pTitle, int[] pIcon) {
        super();
        context = pContext;
        mTitle = pTitle;
        mIcon = pIcon;
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rootView = inflater.inflate(R.layout.navigation_drawer_list_item, parent, false);
    
        TextView txtTitle = (TextView) rootView.findViewById(R.id.drawer_text);
        ImageView imgIcon = (ImageView) rootView.findViewById(R.id.drawer_icon);
    
        if(((ListView)parent).isItemChecked(position)) {
                txtTitle.setTextColor(parent.getResources().getColor(R.color.DarkerRed));
        }
        txtTitle.setText(mTitle[position]);
        imgIcon.setImageResource(mIcon[position]);
    
        return rootView;
    }
    
    @Override
    public int getCount() {
        return mTitle.length;
    }
    
    @Override
    public Object getItem(int position) {
        return mTitle[position];
    }
    
    @Override
    public long getItemId(int position) {
        return position;
    }
    

    }

    在 if 语句 (isItemChecked) 中,您现在可以更改文本视图的背景颜色。

    【讨论】:

    • 嗯,谢谢这个解决方案,我会尽快尝试。但令我惊讶的是,没有一个简单的功能可以做到这一点。
    • 另外看这个问题:stackoverflow.com/q/23396434/3537599
    • 同样的问题,我不敢相信制作该模板的人会创建像 mDrawerListView.addHeaderView(iv); mDrawerListView.addFooterView(iv); 这样的函数,并且不要想象像 mDrawerListView.setHoverItem(Color c); mDrawerListView.setItemTextColor(Color c); 这样的简单函数可能的java代码,并为此创建一个新类太多了。
    • 我终于使用了这个(我没有找到其他解决方案),它的工作。但我不得不更新我的很多代码只是为了这样做。
    【解决方案2】:

    要更改悬停项目背景,我必须更改原色,即项目文本颜色,但我可以更改它

    // change hover text color
    ColorStateList csl = new ColorStateList(
     int[][] {
    
    new int[] {-android.R.attr.state_checked}, // unchecked
    new int[] { android.R.attr.state_checked}  // checked 
    },
    
    new int[] {
    getResources().getColor(R.color.color1, 
    getTheme()), getResources().getColor(R.color.color2, getTheme())
    });
            
    navigationView.setItemTextColor(csl);
    navigationView.setItemIconTintList(csl);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-30
      • 2015-10-23
      • 1970-01-01
      • 2015-11-09
      • 1970-01-01
      • 2017-12-29
      • 1970-01-01
      相关资源
      最近更新 更多