menu.xml中的可绘制矢量图标未显示在appBar中,其中有两个,一个是搜索Icon,另一个是过滤器Icon。我不知道哪里出了错。这是我的活动的摘要:
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
}
请帮助我
答案 0 :(得分:1)
好像我不明白你的问题。尝试将drawable设置为菜单,如下所示:
<item
android:id="@+id/menuItem"
android:icon="@drawable/yourdrawable"
/>
抱歉,下面的文字是关于在菜单中使用animationdrawable的。
带有矢量动画的文件实现了Animatable接口,并且要启动动画,您需要请求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.使用后向动画创建可绘制对象 2.实现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-只是我的布尔标志 抱歉,但是现在我没有时间用Java编写它,但是可以随意提问