我正在尝试在MenuItem上显示工具提示.
我正在使用这个库:从这个Maven存储库编译’com.github.xizzhu:simple-tool-tip:0.5.0’:maven {url“https://jitpack.io”}.
我正在获取MenuItem的视图,将其用作tooltip的锚点,但它在tooltipVIEw.show()方法中启动了nullPointerException.
当我调试应用程序时,查看项目是正确的.
如果我用浮动按钮fab替换VIEw项目,它可以完美地工作.
在某些时候,它工作,但tooltip位置错误,它出现在屏幕的左上角,而不是右侧位置,它位于VIEw项目的底部.
注意:我在OnCreateOptionsMenu()中创建tooltip,因为OnVIEwCreated()在此方法之前运行,所以我必须在那里执行.
这是它发生的地方,如果你需要任何其他代码我可以上传它.
菜单:
<menu xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tooltipproto="http://schemas.androID.com/apk/res-auto"> <item androID:ID="@+ID/action_settings" tooltipproto:actionVIEwClass="androID.Widget.Imagebutton" androID:title="@string/action_settings" tooltipproto:showAsAction="always" /></menu>
分段:
package com.tooltipproto;import androID.os.Bundle;import androID.support.design.Widget.floatingActionbutton;import androID.support.design.Widget.Snackbar;import androID.support.v4.app.Fragment;import androID.vIEw.Gravity;import androID.vIEw.LayoutInflater;import androID.vIEw.Menu;import androID.vIEw.MenuInflater;import androID.vIEw.MenuItem;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import com.github.xizzhu.simpletooltip.tooltip;import com.github.xizzhu.simpletooltip.tooltipVIEw;public class ShowFragment extends Fragment{ VIEw item; floatingActionbutton fab; public ShowFragment(){ } @OverrIDe public voID onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); inflater.inflate(R.menu.menu_main, menu); MenuItem menuItem = menu.findItem(R.ID.action_settings); item = menuItem.getActionVIEw(); tooltip tooltip = new tooltip.Builder() .withText("Simple Tool Tip!") .withTextSize(50) .withpadding(10,10,10,10) .withCornerRadius(10) .build(); final tooltipVIEw tooltipVIEw = new tooltipVIEw.Builder(getActivity()) .withAnchor(item) .withtooltip(tooltip) .withGravity(Gravity.BottOM) .build(); tooltipVIEw.show(); } @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup parent, Bundle savedInstanceState) { // defines the xml file for the fragment setHasOptionsMenu(true); fab = (floatingActionbutton) getActivity().findVIEwByID(R.ID.fab); fab.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { Snackbar.make(vIEw, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); setHasOptionsMenu(true); return inflater.inflate(R.layout.fragment, parent, false); }}
解决方法:
方法1:
首先创建自定义工具栏
工具提示库链接Visit
activity_login.xml
<?xml version="1.0" enCoding="utf-8"?><androID.support.design.Widget.CoordinatorLayoutxmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:app="http://schemas.androID.com/apk/res-auto"xmlns:banner="http://schemas.androID.com/apk/res-auto"xmlns:tools="http://schemas.androID.com/tools"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:fitsSystemwindows="true"androID:background="@mipmap/background"tools:context="com.and.postanads.LoginActivity"><androID.support.design.Widget.AppbarLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:theme="@style/Apptheme.AppbarOverlay"> <androID.support.v7.Widget.Toolbar androID:ID="@+ID/toolbar" androID:layout_wIDth="match_parent" androID:layout_height="?attr/actionbarSize" androID:background="?attr/colorPrimary" app:popuptheme="@style/Apptheme.PopupOverlay"> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textAppearance="?androID:attr/textAppearanceSmall" androID:text="SKIP" androID:textcolor="#FFFFFF" androID:textStyle="bold" androID:FontFamily="sans-serif" androID:ID="@+ID/txt_skip" androID:layout_marginRight="15dp" androID:layout_gravity="right"/> </androID.support.v7.Widget.Toolbar></androID.support.design.Widget.AppbarLayout>
LoginActivity.java
public class LoginActivity extends AppCompatActivity implements tooltip.Callback {private tooltip.ClosePolicy mClosePolicy = tooltip.ClosePolicy.touch_ANYWHERE_CONSUME;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_login); Toolbar toolbar = (Toolbar) findVIEwByID(R.ID.toolbar); setSupportActionbar(toolbar); tooltip.dbg = true; TextVIEw textVIEw = (TextVIEw)findVIEwByID(R.ID.txt_skip); tooltip.make(this, new tooltip.Builder(101) .anchor(textVIEw, tooltip.Gravity.BottOM) .closePolicy(mClosePolicy, 5000) .text("tooltip on a TabLayout child...tooltip on a TabLayout child...") .withStyleID(R.style.tooltipLayoutDefaultStyle_CustomFont) .fadeDuration(200) .fitToScreen(true) .activateDelay(2000) .withCallback(this) .floatingAnimation(tooltip.AnimationBuilder.DEFAulT) .showDelay(400) .build() ).show(); }}
Sample image
方法2:
使用Menu_item
menu_login.xml
<?xml version="1.0" enCoding="utf-8"?><menu xmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:app="http://schemas.androID.com/apk/res-auto"><item androID:ID="@+ID/menu_skip" androID:orderIncategory="100" androID:title="Skip" app:showAsAction="always"/></menu>
LoginActivity.java
public class LoginActivity extends AppCompatActivity implements tooltip.Callback {private tooltip.ClosePolicy mClosePolicy = tooltip.ClosePolicy.touch_ANYWHERE_CONSUME;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_login); Toolbar toolbar = (Toolbar) findVIEwByID(R.ID.toolbar); setSupportActionbar(toolbar); tooltip.dbg = true;}@OverrIDepublic boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_login, menu); return true;}@OverrIDepublic boolean onoptionsItemSelected(MenuItem item) { int ID = item.getItemID(); if (ID == R.ID.menu_skip) { VIEw vIEw = findVIEwByID(R.ID.menu_skip); tooltip.make(this, new tooltip.Builder(101) .anchor(vIEw, tooltip.Gravity.BottOM) .closePolicy(mClosePolicy, 5000) .text("tooltip on a TabLayout child...tooltip on a TabLayout child...") .fadeDuration(200) .fitToScreen(true) .activateDelay(2000) .withCallback(this) .floatingAnimation(tooltip.AnimationBuilder.DEFAulT) .showDelay(400) .build() ).show(); return true; } return super.onoptionsItemSelected(item);}}
总结 以上是内存溢出为你收集整理的MenuItem上的Android工具提示全部内容,希望文章能够帮你解决MenuItem上的Android工具提示所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)