gpt4 book ai didi

android - menu.xml 中定义的可绘制矢量图标未显示在 appBar 中?

转载 作者:行者123 更新时间:2023-11-29 18:48:13 24 4
gpt4 key购买 nike

menu.xml中的drawable vector icon并没有出现在appBar中,有两个,一个是搜索icon,一个是filter icon。我不知道我哪里错了。这是我的 Activity fragment :

static String query;
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
Toolbar myToolbar;
SearchView searchView = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
//Set custom AppBar
myToolbar = findViewById(R.id.search_toolbar);
//myToolbar.inflateMenu(R.menu.menu_search);
initToolBarMenuIcons();
setSupportActionBar(myToolbar);

myToolbar.setOnMenuItemClickListener(this);
setupViews();

//Check internet connection
new InternetCheck(internet -> {
//Display no internet Toast if there's no internet
if (!internet) {
Toast.makeText(this, "no internet", Toast.LENGTH_SHORT).show();
}
});
}

@MainThread
@SuppressLint("NewApi") //suppress warning, since using DrawableCompat to set tint
public void initToolBarMenuIcons() {
myToolbar.inflateMenu(R.menu.menu_search);
//Manually adding icon since it's a vector drawable and we can't currently inflate from XML into menuitems
Drawable wrappedFilterIcon = DrawableCompat
.wrap(Objects.requireNonNull(AppCompatResources
.getDrawable(getBaseContext(), R.drawable.ic_filter_list_white_24dp)));
Drawable wrappedSearchIcon = DrawableCompat
.wrap(Objects.requireNonNull(AppCompatResources
.getDrawable(getBaseContext(), R.drawable.ic_search_white_24dp)));
//Tint it too
DrawableCompat.setTint(wrappedFilterIcon, Color.WHITE);
DrawableCompat.setTint(wrappedSearchIcon, Color.WHITE);
//Set the tinted vector drawable to the item
myToolbar.getMenu().findItem(R.id.action_filter).setIcon(wrappedFilterIcon);
myToolbar.getMenu().findItem(R.id.action_search).setIcon(wrappedSearchIcon);
}
[snip...]

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
onArticleSearch(null);
searchView.clearFocus();
return true;
}

@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return super.onCreateOptionsMenu(menu);
}

这是指定项目和图标源的 menu.xml。我使用 app:srcCompat 作为图标路径。

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_filter"
app:showAsAction="always"
app:srcCompat="@drawable/ic_filter_list_white_24dp"
tools:ignore="MenuTitle"
android:orderInCategory="2" />

<item android:id="@+id/action_search"
android:orderInCategory="1"
android:title="Search Article"
app:srcCompat="@drawable/ic_search_white_24dp"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />

我导入了以下库:实现“com.android.support:support-vector-drawable:27.1.1” 实现“com.android.support:animated-vector-drawable:27.1.1”

我在我的应用程序 Gradle 文件中设置了它:

android {
compileSdkVersion 27
defaultConfig {
applicationId "com.realty.drake.newyorktimessearcher"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary true
}

请帮帮我

最佳答案

看来我不明白你的问题。尝试像这样将 drawable 设置为您的菜单:

  <item
android:id="@+id/menuItem"
android:icon="@drawable/yourdrawable"
/>

对不起兄弟,下面的文字是关于在菜单中使用animateddrawable。

矢量动画文件实现Animatable接口(interface),启动动画需要请求Drawable,将其带入Animatable,调用start()方法。我在 onOptionsItemSelected 中这样做:

  override fun onOptionsItemSelected(item: MenuItem?): Boolean {
when (item?.itemId) {
..........
R.id.editNotification -> {
(item.icon as Animatable).start()
}
}
return super.onOptionsItemSelected(item)
}

对于 Java 使用:

((Animatable) item.getIcon()).start();

但是您可能遇到了我遇到过的问题:如何向后设置动画。我没有本地解决方案,但你可以这样做:1.创建带倒退动画的drawable2. 实现 AnimationCallback 并在动画结束时切换那些可绘制对象。例如:

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_edit_notification, menu)
animDrawable = AnimatedVectorDrawableCompat.create(this, R.drawable.avd_edit_to_close)!!
menu?.getItem(0)?.icon = animDrawable
return true
}

override fun onOptionsItemSelected(item: MenuItem?): Boolean {
when (item?.itemId) {
..............................
R.id.editNotification -> {
animDrawable.registerAnimationCallback(object : Animatable2Compat.AnimationCallback() {
override fun onAnimationEnd(drawable: Drawable?) {
animDrawable = AnimatedVectorDrawableCompat.create(this@NotificationActivity, if (inEditMode) R.drawable.avd_edit_to_close else R.drawable.avd_close_to_edit)!!
inEditMode = !inEditMode
item.icon = animDrawable
}
})
animDrawable.start()
}
}
return super.onOptionsItemSelected(item)
}

其中:animDrawable - 是 AnimatedVectorDrawableCompat 的实例 inEditMode - 只是我的 bool 标志对不起,但现在我没有时间用 Java 编写它,但有问题可以自由提问

关于android - menu.xml 中定义的可绘制矢量图标未显示在 appBar 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51366374/

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