本文介绍了如何将Lottie文件设置为菜单项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建底部导航,并且图标必须是动画的Lottie文件.似乎无法为该标签编写绑定适配器.有什么解决方案可以让我们在底部导航中放置抽奖动画吗?
I am creating a bottom navigation and the icon needs to be an animated Lottie file.It seems it's not possible to write a binding adapter for the tag. Is there any solution that allows us to have lottie animations in bottom nav?
<item
android:id="@+id/my_navigation"
android:icon="@drawable/my_icon"
android:title="@string/my_text"
**app:setLottie=@raw/my_file/>**
推荐答案
首先创建一个自定义布局,在此示例中将其命名为layout_gift
First of all create a custom layout, in this example name it layout_gift
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="30dp"
android:layout_height="30dp"
android:orientation="horizontal">
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/watch_animation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:lottie_autoPlay="true"
app:lottie_fileName="gift.json"
app:lottie_loop="true"
app:lottie_speed="4" />
</LinearLayout>
然后将其设置为;
<item
android:id="@+id/action_gift"
android:orderInCategory="100"
android:title="@string/gift"
app:actionLayout="@layout/layout_gift"
app:showAsAction="always" />
此外,clickListener如下所示,您无法在onOptionsItemSelected中处理它
Moreover, clickListener is as below, you can't handle it in onOptionsItemSelected
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
MenuItem giftItem = menu.findItem(R.id.action_gift);
View giftView = giftItem.getActionView();
giftView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// todo
}
});
}
这篇关于如何将Lottie文件设置为菜单项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!