Activity+Toolbar+ActionMode 女爷i 2022-06-16 02:11 175阅读 0赞 Android中有时会用到这样的搭配:Activity(android.app.Activity)+ Toolbar(android.support.v7.widget.Toolbar)+ ActionMode(使用ListView时)。在运用过程中,难免会出现各种各样的问题,这里给大家看一下我的用法。 activity\_main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#78b7ea"> </android.support.v7.widget.Toolbar> <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </LinearLayout> MainActivity.java: package com.example.taademo; import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.view.ActionMode; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AbsListView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity { private String[] fruits = {"pear", "apple", "watermelon", "banana", "tomato"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setTitle("你好,世界"); toolbar.setNavigationIcon(R.drawable.back); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); ListView listView = (ListView) findViewById(android.R.id.list); listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() { @Override public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {} @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { // 填充menu mode.getMenuInflater().inflate(R.menu.my_menu, menu); return true; } @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { // 长按listview的item时,方法被调用 switch(item.getItemId()) { case R.id.delete: Toast.makeText(MainActivity.this, "李时珍的皮", Toast.LENGTH_SHORT).show(); } return true; } @Override public void onDestroyActionMode(ActionMode mode) {} }); ArrayAdapter<String> adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, fruits); listView.setAdapter(adapter); } } my\_menu.xml: <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/delete" android:title="删除" android:icon="@drawable/delete"> </item> </menu> 运行后的效果是: ![Center][] 可以看到功能是实现了的,但是有个问题,长按ListView中的item后出现的ActionMode会把Toolbar压下去。 办法是修改styles: <resources> <!-- Base application theme. --> <style name="AppTheme" parent="MyTheme"> </style> <style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- 设置action mode是否覆盖 --> <item name="android:windowActionModeOverlay">true</item> <!-- 设置action mode背景颜色 --> <item name="android:actionModeBackground">@color/orange</item> <!-- 设置action mode退出按钮 --> <item name="android:actionModeCloseDrawable">@drawable/back</item> </style> </resources> 运行效果: ![Center 1][] 可以看到把Toolbar压下去的情况不存在了,到这里在本机上实现了功能。 有些情况下Toolbar与ActionMode会出现高度不一致的情况,就是Toolbar的宽度会大一点 修改方法是将activity\_main.xml中Toolbar的: android:layout\_height="wrap\_content" 改成 android:layout\_height="?android:attr/actionBarSize" [Center]: /images/20220616/de2b81c882cf4ddcb26d6fc9eaaee96a.png [Center 1]: /images/20220616/066dd03f63f047b3b1a95dc353278111.png
还没有评论,来说两句吧...