menu.xml 中定义的可绘制矢量图标未显示在 appBar 中?
Drawable Vector Icon defined in menu.xml doesn't show up in appBar?
menu.xml中的可绘制矢量图标没有出现在appBar中,有两个,一个是搜索图标,一个是过滤图标。我不知道我哪里错了。这是我 Activity:
的片段
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
}
请大家帮帮我
我好像不明白你的问题。尝试像这样为您的菜单设置可绘制对象:
<item
android:id="@+id/menuItem"
android:icon="@drawable/yourdrawable"
/>
对不起兄弟,下面的文字是关于在菜单中使用animateddrawable。
矢量动画文件实现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.创建带倒退动画的drawable
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,有问题可以留言
menu.xml中的可绘制矢量图标没有出现在appBar中,有两个,一个是搜索图标,一个是过滤图标。我不知道我哪里错了。这是我 Activity:
的片段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
}
请大家帮帮我
我好像不明白你的问题。尝试像这样为您的菜单设置可绘制对象:
<item
android:id="@+id/menuItem"
android:icon="@drawable/yourdrawable"
/>
对不起兄弟,下面的文字是关于在菜单中使用animateddrawable。
矢量动画文件实现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.创建带倒退动画的drawable 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,有问题可以留言