gpt4 book ai didi

android - 工具栏中的自定义菜单项

转载 作者:行者123 更新时间:2023-11-29 19:46:29 25 4
gpt4 key购买 nike

我正在尝试为我的项目菜单创建自定义布局。我需要一个带有图像的图标来显示这样的购物车:

image

我创建了一个自定义的相对布局:

<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>

有人知道怎么做吗?

提前致谢

编辑:

主 Activity .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);

}

}

最佳答案

如果你想在工具栏中使用计数器,试试 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);
}

关于android - 工具栏中的自定义菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37544636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com