在res/menu上附加android:actionLayout如何从.xml调用到.java
我对res/menu.xml文件夹有点困惑 我想在我的操作栏上有一个购物车,有textview计数器(每次点击都会递增)。我有一个RelativeLayout,其中包含名为在res/menu上附加android:actionLayout如何从.xml调用到.java,java,android,xml,Java,Android,Xml,我对res/menu.xml文件夹有点困惑 我想在我的操作栏上有一个购物车,有textview计数器(每次点击都会递增)。我有一个RelativeLayout,其中包含名为badge\u counter\u text的ImageView和TextView。我想将其附加到res/menu/main.xml上 即: <item android:id="@+id/rightSlider" android:orderInCategory="100"
badge\u counter\u text
的ImageView和TextView。我想将其附加到res/menu/main.xml上
即:
<item
android:id="@+id/rightSlider"
android:orderInCategory="100"
android:actionLayout="@layout/badge_counter_text"
android:showAsAction="always"
android:actionViewClass="android.widget.ImageButton"
android:icon="@drawable/cart"
android:title="Cart"/>
问题是我的txtCount
对象上的值为空。尝试使用
为此目的。您可以像这样添加自己的布局
<android.support.v7.widget.Toolbar
android:id="@+id/nav_toolbar"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blue">
<ImageButton
android:id="@+id/btn_toolbar_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="@dimen/spacing_tiny"
android:background="@drawable/filter_nav_prev"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<TextView
android:id="@+id/toolbar_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:textAllCaps="false"
android:textColor="@color/background_white"
android:textSize="@dimen/font_big"/>
</RelativeLayout>
对不起,我忘记了一些。实际上我需要一个正确的java代码,因为我在上面的java代码中遇到了错误。谢谢你的建议,但我的问题是我在代码中哪里做错了(请查看我的java代码)好的,那么显示你的xml for badge\u layout\u cartCounter菜单项。我真的很麻烦,我真的建议你们使用工具栏,它在这种情况下很有用。但不管怎样,你提到你的txtCount得到空值。请显示您的菜单项,因为如果您没有菜单项,findItem(R.id.badge\u layout\u cartCounter)将返回Null我已经附加了另一个布局,您可以查看它,兄弟(先生)
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
btnCart = (ImageButton)menu.findItem(R.id.rightSlider).getActionView();
txtCount = (TextView)menu.findItem(R.id.badge_layout_cartCounter).getActionView().findViewById(R.id.add_item_counter);
//...
}
<android.support.v7.widget.Toolbar
android:id="@+id/nav_toolbar"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blue">
<ImageButton
android:id="@+id/btn_toolbar_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="@dimen/spacing_tiny"
android:background="@drawable/filter_nav_prev"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<TextView
android:id="@+id/toolbar_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:textAllCaps="false"
android:textColor="@color/background_white"
android:textSize="@dimen/font_big"/>
</RelativeLayout>
ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.badge_layout_cartCounter, null);
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);