提问人:randomdood1923 提问时间:7/5/2023 更新时间:7/5/2023 访问量:24
向支持 ActionBar 添加按钮
Adding a button to the support ActionBar
问:
我目前为我的应用程序设置了这个设置,其中我在屏幕顶部有一个操作栏,左上角有一个“汉堡包”图标,显示这个导航抽屉。
我现在想在操作栏的右上角添加一个图标,该图标将显示带有徽章的购物车。不幸的是,我似乎没有找到一个可行的解决方案,因为我所看到的大多数其他问题的答案似乎都过时了。特别是,我尝试的是添加另一个菜单选项,该选项将“app:showAsAction”设置为始终,并通过覆盖我的活动来“设置”。问题是 1.该项目也显示在导航抽屉中,我不想要 2.这将创建一个选项菜单,这意味着我得到了这些永久三个点的存在:onCreateOptionsMenu(Menu menu)
我想知道一种替代解决方案,它让我在左侧的操作栏中有一个带有常规“汉堡包”图标的导航抽屉,在右上角有一个购物车按钮(按钮,而不仅仅是一个图标,我需要在点击时有行为)。这是我的项目的样子:
menu_entries.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/navMenu"
tools:ignore="HardcodedText">
<item
android:id="@+id/navMenuCocktail"
android:title="Cocktail" />
<item
android:id="@+id/navMenuFrullati"
android:title="Frullati" />
<item
android:id="@+id/navMenuSuggested"
android:title="Suggeriti" />
<item
android:id="@+id/navMenuLogout"
android:title="Logout"/>
<item
android:id="@+id/navMenuActionCart"
android:title="Cart"
android:enabled="false"
app:actionLayout="@layout/cart_item_layout"
android:icon="@drawable/ic_action_cart"
app:showAsAction="always"/>
</menu>
layout_home.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerLayoutHome"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="HardcodedText">
<ProgressBar
android:id="@+id/progressBarHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleX="0.5"
android:scaleY="0.5"
android:indeterminateTint="@color/drink_orange"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewDrink"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navViewMenu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/menu_entries"/>
</androidx.drawerlayout.widget.DrawerLayout>
控制器主页 .java (显示的活动。我注释掉了很多无关紧要的东西,比如网络和导航抽屉逻辑。
//
public class ControllerHome extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener {
private ArrayList<FeedItem> cartFeedItems = new ArrayList<>();
private enum HomeState{
HOME_COCKTAIL,
HOME_FRULLATI,
HOME_SUGGESTED,
HOME_CART,
HOME_LOGOUT;
@Override
public String toString(){
if (this.equals(HOME_COCKTAIL)) return "Cocktail";
else if (this.equals(HOME_FRULLATI)) return "Frullati";
else if (this.equals(HOME_SUGGESTED)) return "Consigliati";
else if (this.equals(HOME_LOGOUT)) return "Logout";
else if (this.equals(HOME_CART)) return "Carrello";
return "INVALID";
}
}
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
private NavigationView navigationView;
private HomeState currentHomeState = HomeState.HOME_COCKTAIL;
//
private RecyclerView recyclerViewDrink;
private FeedItemAdapter feedItemAdapter;
private ProgressBar progressBar;
private TextView cartBadge;
private int numberOfItemsInCart = 0;
private final int MAX_NUMBER_OF_CART_ITEMS_DISPLAYED = 99;
private FeedItemAdapter.ProductQuantityListener
productQuantityListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_home);
drawerLayout = findViewById(R.id.drawerLayoutHome);
navigationView = findViewById(R.id.navViewMenu);
recyclerViewDrink = findViewById(R.id.recyclerViewDrink);
progressBar = findViewById(R.id.progressBarHome);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.nav_open, R.string.nav_close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
navigationView.setNavigationItemSelectedListener(this);
//
}
@Override
public boolean onCreateOptionsMenu(@NonNull Menu menu){
getMenuInflater().inflate(R.menu.menu_entries, menu);
MenuItem menuItem = menu.findItem(R.id.navMenuActionCart);
View actionView = menuItem.getActionView();
cartBadge = actionView.findViewById(R.id.cart_badge);
actionView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNavigationItemSelected(menuItem);
}
});
cartBadge = findViewById(R.id.cart_badge);
cartBadge.setVisibility(View.GONE);
productQuantityListener = //
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
//
}
答: 暂无答案
评论