工具栏中的自定义菜单项
Custom menu item in Toolbar
我正在尝试为我的项目菜单创建自定义布局。我需要一个带有图像的图标来显示这样的购物车:
我创建了自定义相对布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="48dp"
android:layout_height="fill_parent"
android:layout_gravity="right" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:clickable="true"
android:src="@drawable/ic_shopping_cart"/>
<TextView
android:id="@+id/counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="8dp"
android:text="5"
android:textSize="15sp"
android:textColor="#ffffff" />
</RelativeLayout>
以及项目菜单:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/icon_shopping_cart"
android:actionLayout="@layout/shopping_cart"
android:title="@string/shopping_icon_description"
app:showAsAction="always" />
</menu>
有人知道怎么做吗?
提前致谢
编辑:
MainActivity.java
public class Prueba extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_agroquimicos);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.inflateMenu(R.id.shopping_cart);
}
}
我会将您的购物车保存为图片并按如下方式实现您的菜单布局:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/icon_shopping_cart"
android:icon="@drawable/shopping_cart"
android:title="@string/shopping_icon_description"
android:showAsAction="always" />
</menu>
然后给它充气 activity / 片段:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_layout, menu);
}
如果你想在工具栏中使用计数器,试试 ActionBarMenuItemCounter,它对我有用
private Drawable buildCounterDrawable(int count, int backgroundImageId) {
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.counter_menuitem_layout, null);
view.setBackgroundResource(backgroundImageId);
if (count == 0) {
View counterTextPanel = view.findViewById(R.id.counterValuePanel);
counterTextPanel.setVisibility(View.GONE);
} else {
TextView textView = (TextView) view.findViewById(R.id.count);
textView.setText("" + count);
}
view.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
return new BitmapDrawable(getResources(), bitmap);
}
你的设置是正确的,现在只有下面是你需要的东西..
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.your_menu_file, menu);
final MenuItem item = menu.findItem(R.id.icon_shopping_cart);
TextView cartCount = (TextView) item.getActionView().findViewById(R.id.counter);
cartCount.setText("10");
return true;
}
并删除这一行
toolbar.inflateMenu(R.id.shopping_cart);
我正在尝试为我的项目菜单创建自定义布局。我需要一个带有图像的图标来显示这样的购物车:
我创建了自定义相对布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="48dp"
android:layout_height="fill_parent"
android:layout_gravity="right" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:clickable="true"
android:src="@drawable/ic_shopping_cart"/>
<TextView
android:id="@+id/counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="8dp"
android:text="5"
android:textSize="15sp"
android:textColor="#ffffff" />
</RelativeLayout>
以及项目菜单:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/icon_shopping_cart"
android:actionLayout="@layout/shopping_cart"
android:title="@string/shopping_icon_description"
app:showAsAction="always" />
</menu>
有人知道怎么做吗?
提前致谢
编辑:
MainActivity.java
public class Prueba extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_agroquimicos);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.inflateMenu(R.id.shopping_cart);
}
}
我会将您的购物车保存为图片并按如下方式实现您的菜单布局:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/icon_shopping_cart"
android:icon="@drawable/shopping_cart"
android:title="@string/shopping_icon_description"
android:showAsAction="always" />
</menu>
然后给它充气 activity / 片段:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_layout, menu);
}
如果你想在工具栏中使用计数器,试试 ActionBarMenuItemCounter,它对我有用
private Drawable buildCounterDrawable(int count, int backgroundImageId) {
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.counter_menuitem_layout, null);
view.setBackgroundResource(backgroundImageId);
if (count == 0) {
View counterTextPanel = view.findViewById(R.id.counterValuePanel);
counterTextPanel.setVisibility(View.GONE);
} else {
TextView textView = (TextView) view.findViewById(R.id.count);
textView.setText("" + count);
}
view.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
return new BitmapDrawable(getResources(), bitmap);
}
你的设置是正确的,现在只有下面是你需要的东西..
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.your_menu_file, menu);
final MenuItem item = menu.findItem(R.id.icon_shopping_cart);
TextView cartCount = (TextView) item.getActionView().findViewById(R.id.counter);
cartCount.setText("10");
return true;
}
并删除这一行
toolbar.inflateMenu(R.id.shopping_cart);