gpt4 book ai didi

android - 抽屉导航中的图像颜色发生变化

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:30:12 26 4
gpt4 key购买 nike

我在抽屉导航方面遇到了一个奇怪的问题。我使用 Android Studio 提供的默认模板创建了抽屉导航,它使用 menu.xml 文件创建了它。现在的问题是,每当我将图像添加到抽屉导航列表项时,尽管有任何彩色图像,它的颜色都会变为灰色。这是抽屉导航中的一张图片

icon

如您所见,图像的颜色是绿色,但添加到抽屉导航后,它看起来像 Navigation Drawer

不知道是什么原因造成的

这里是menu.xml

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<group android:checkableBehavior="single">
<item
android:id="@+id/nav_camera"
android:icon="@drawable/clock"
android:title="Home" />
<item
android:id="@+id/nav_gallery"
android:icon="@drawable/mega_event"
android:title="Mega Events" />
<item
android:id="@+id/nav_slideshow"
android:icon="@drawable/tickets"
android:title="My Tickets" />
<item
android:id="@+id/nav_manage"
android:icon="@drawable/profile"
android:title="Profile" />
<item
android:id="@+id/nav_settings"
android:icon="@drawable/settings"
android:title="Settings" />
<item
android:id="@+id/nav_logout"
android:icon="@drawable/logout"
android:title="Logout" />
</group>

</menu>

这里第一项的drawable就是我贴的那个

及 Activity 主题

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>

和抽屉导航 Activity 类

    public class ActivityNavigationDrawer extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawer);
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.setDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setBackgroundColor(Color.parseColor("#FFFFFF"));
navigationView.setNavigationItemSelectedListener(this);
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView,new FragmentTabs()).commit();
}

@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.
return true;
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();

if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {

} else if (id == R.id.nav_slideshow) {

} else if (id == R.id.nav_manage) {

}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}

最佳答案

您可以直接使用 app:itemIconTint 更改图标颜色,使用 app:itemTextColor 更改导航 View 中的文本项颜色。

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Content Layout -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/adView"
android:layout_below="@id/my_awesome_toolbar" />

<!-- Navigation Drawer -->
<android.support.design.widget.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemIconTint="#FF0000"
app:itemTextColor="#D2691E"
app:menu="@menu/navigation_menu" />
</android.support.v4.widget.DrawerLayout>

关于android - 抽屉导航中的图像颜色发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34579513/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com