java– 更改navigationView的单个特定菜单项的背景颜色

java– 更改navigationView的单个特定菜单项的背景颜色,第1张

概述我想在导航抽屉里的android菜单中设置所有标题项的背景颜色.我的布局看起来像:<menuxmlns:android="http://schemas.android.com/apkes/android"><itemandroid:title="TopItem"android:id="@+idop_item1">//Hereiwanttosetthebackground&l

我想在导航抽屉里的android菜单中设置所有标题项的背景颜色.我的布局看起来像:

<menu xmlns:androID="http://schemas.androID.com/apk/res/androID">  <item androID:title="topItem" androID:ID="@+ID/top_item1"> // Here i want to set the background    <menu>      <group>        <item androID:ID="@+ID/sub_item1"            androID:title="SubItem" /> // Here no background      </group>    </menu>  </item>  <item androID:title="topItem" androID:ID="@+ID/top_item2">    <menu>      <group>       <item androID:ID="@+ID/sub_item2"            androID:title="SubItem" />        <item androID:ID="@+ID/sub_item3"            androID:title="SubItem" />       <item androID:ID="@+ID/sub_item4"            androID:title="SubItem" />      </group>    </menu></menu>

结果应该看起来像:

我发现我可以使用以下内容设置文本颜色:

MenuItem menuItem = navigationVIEw.getMenu().findItem(R.ID.menu_item);SpannableString s = new SpannableString(menuItem.getTitle());s.setSpan(new TextAppearanceSpan(this, R.style.TextAppearance), 0, s.length(), 0);if (menuItem.getItemID()==R.ID.nav_targets){              menuItem.setTitle(s); }

但是我如何设置填充背景颜色?

解决方法:

Change background color of single specific menu items

AFAIK使用菜单这是不可能的,您需要创建自定义navigationVIEw

当您使用BackgroundcolorSpan为菜单项设置背景时,它仅将背景设置为菜单项标题而不是整个视图

OUTPUT USING BackgroundcolorSpan

Try this way using RecyclerVIEw

activity_main.xml

<?xml version="1.0" enCoding="utf-8"?><androID.support.v4.Widget.DrawerLayout 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:ID="@+ID/drawer_layout"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:fitsSystemwindows="true"    tools:openDrawer="start">    <include        layout="@layout/app_bar_main"        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent" />    <androID.support.design.Widget.NavigationVIEw        androID:ID="@+ID/nav_vIEw"        androID:layout_wIDth="wrap_content"        androID:layout_height="match_parent"        androID:layout_gravity="start"        androID:fitsSystemwindows="true">        <linearLayout            androID:layout_wIDth="match_parent"            androID:layout_height="match_parent"            androID:orIEntation="vertical">            <include layout="@layout/nav_header_main" />            <androID.support.v7.Widget.RecyclerVIEw                androID:ID="@+ID/navRecyclerVIEw"                androID:layout_wIDth="match_parent"                androID:layout_height="wrap_content" />        </linearLayout>    </androID.support.design.Widget.NavigationVIEw></androID.support.v4.Widget.DrawerLayout>

MainActivity

public class MainActivity extends AppCompatActivity        implements NavigationVIEw.OnNavigationItemSelectedListener {    RecyclerVIEw navRecyclerVIEw;    linearlayoutmanager layoutManager;    ArrayList<NavigationDataModel> arrayList = new ArrayList<>();    NavigationAdapter adapter;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        Toolbar toolbar = (Toolbar) findVIEwByID(R.ID.toolbar);        setSupportActionbar(toolbar);        DrawerLayout drawer = (DrawerLayout) findVIEwByID(R.ID.drawer_layout);        ActionbarDrawerToggle toggle = new ActionbarDrawerToggle(                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);        drawer.addDrawerListener(toggle);        toggle.syncState();        NavigationVIEw navigationVIEw = (NavigationVIEw) findVIEwByID(R.ID.nav_vIEw);        navigationVIEw.setNavigationItemSelectedListener(this);        navRecyclerVIEw = findVIEwByID(R.ID.navRecyclerVIEw);        navRecyclerVIEw.setHasFixedSize(true);        layoutManager = new linearlayoutmanager(this);        navRecyclerVIEw.setLayoutManager(layoutManager);        initArray();        adapter = new NavigationAdapter(this, arrayList);        navRecyclerVIEw.setAdapter(adapter);    }    private voID initArray() {        NavigationDataModel model = new NavigationDataModel();        model.setcolor(ContextCompat.getcolor(this, R.color.colorPrimary));        model.setIcon(R.drawable.ic_menu_gallery);        model.setTitle("Item 1");        arrayList.add(model);        NavigationDataModel model2 = new NavigationDataModel();        model2.setcolor(ContextCompat.getcolor(this, R.color.colorRed));        model2.setIcon(R.drawable.ic_menu_camera);        model2.setTitle("Item 2");        arrayList.add(model2);        NavigationDataModel model3 = new NavigationDataModel();        model3.setcolor(ContextCompat.getcolor(this, R.color.colorGreen));        model3.setIcon(R.drawable.ic_menu_send);        model3.setTitle("Item 3");        arrayList.add(model3);        NavigationDataModel model4 = new NavigationDataModel();        model4.setcolor(ContextCompat.getcolor(this, R.color.colorPink));        model4.setIcon(R.drawable.ic_menu_share);        model4.setTitle("Item 4");        arrayList.add(model4);    }    @OverrIDe    public voID onBackpressed() {        DrawerLayout drawer = (DrawerLayout) findVIEwByID(R.ID.drawer_layout);        if (drawer.isDrawerOpen(GravityCompat.START)) {            drawer.closeDrawer(GravityCompat.START);        } else {            super.onBackpressed();        }    }    @OverrIDe    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @OverrIDe    public boolean onoptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroIDManifest.xml.        int ID = item.getItemID();        //noinspection SimplifiableIfStatement        if (ID == R.ID.action_settings) {            return true;        }        return super.onoptionsItemSelected(item);    }    @SuppressWarnings("StatementWithEmptyBody")    @OverrIDe    public boolean onNavigationItemSelected(MenuItem item) {        // Handle navigation vIEw item clicks here.        int ID = item.getItemID();        DrawerLayout drawer = (DrawerLayout) findVIEwByID(R.ID.drawer_layout);        drawer.closeDrawer(GravityCompat.START);        return true;    }}

NavigationAdapter

public class NavigationAdapter extends RecyclerVIEw.Adapter<NavigationAdapter.VIEwHolder> {    Context context;    ArrayList<NavigationDataModel> arrayList = new ArrayList<>();    public NavigationAdapter(Context context, ArrayList<NavigationDataModel> arrayList) {        this.context = context;        this.arrayList = arrayList;    }    @NonNull    @OverrIDe    public VIEwHolder onCreateVIEwHolder(@NonNull VIEwGroup parent, int vIEwType) {        VIEw vIEw = LayoutInflater.from(context).inflate(R.layout.custom_layout, parent, false);        return new VIEwHolder(vIEw);    }    @OverrIDe    public voID onBindVIEwHolder(@NonNull VIEwHolder holder, int position) {        holder.navIcon.setimageResource(arrayList.get(position).getIcon());        holder.rootVIEw.setBackgroundcolor(arrayList.get(position).getcolor());        holder.navTitle.setText(arrayList.get(position).getTitle());    }    @OverrIDe    public int getItemCount() {        return arrayList.size();    }    public class VIEwHolder extends RecyclerVIEw.VIEwHolder {        ImageVIEw navIcon;        TextVIEw navTitle;        linearLayout rootVIEw;        public VIEwHolder(VIEw itemVIEw) {            super(itemVIEw);            rootVIEw = itemVIEw.findVIEwByID(R.ID.rootVIEw);            navIcon = itemVIEw.findVIEwByID(R.ID.navIcon);            navTitle = itemVIEw.findVIEwByID(R.ID.navTitle);        }    }}

NavigationDataModel

public class NavigationDataModel {    private int icon, color;    private String Title;    public int getIcon() {        return icon;    }    public voID setIcon(int icon) {        this.icon = icon;    }    public int getcolor() {        return color;    }    public voID setcolor(int color) {        this.color = color;    }    public String getTitle() {        return Title;    }    public voID setTitle(String Title) {        this.Title = Title;    }}

OUTPUT


总结

以上是内存溢出为你收集整理的java – 更改navigationView的单个特定菜单项的背景颜色全部内容,希望文章能够帮你解决java – 更改navigationView的单个特定菜单项的背景颜色所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1104071.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存