android(10) 左侧菜单栏
一.左侧菜单栏:
下载了一份源码,看见了这种效果,其实就是显示隐藏,以后肯定能用的到,稍微改了一下,加了点注释,所以记录一下(要代码的留下邮箱)。
效果图:
主界面:
public class MainActivity extends Activity implements MenuItemCallback { private MenuView rbmView; private Button button1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = (Button)findViewById(R.id.button1); rbmView = (MenuView) findViewById(R.id.ribbonMenuView1); rbmView.setMenuClickCallback(this); rbmView.setMenuItems(R.menu.ribbon_menu); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { Toast.makeText(MainActivity.this,"我只是个没有用的按钮!", Toast.LENGTH_SHORT).show(); } }); //图标可以点击 getActionBar().setDisplayHomeAsUpEnabled(true); } //点击menu时调用 @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == android.R.id.home) { rbmView.toggleMenu(); return true; } else { return super.onOptionsItemSelected(item); } } @Override public void MenuItemClick(int itemId) { Toast.makeText(this, "点击",Toast.LENGTH_SHORT).show(); } }MenuView:
public class MenuView extends LinearLayout { private ListView rbmListView; private View rbmOutsideView; private MenuItemCallback callback; private static ArrayList<RibbonMenuItem> menuItems; public MenuView(Context context) { super(context); load(); } public MenuView(Context context, AttributeSet attrs) { super(context, attrs); load(); } //加菜 private void load() { if (isInEditMode()) return; inflateLayout(); initUi(); } // 初始化布局 private void inflateLayout() { try { LayoutInflater.from(getContext()).inflate( R.layout.activity_menu, this, true); } catch (Exception e) { } } //初始化ui private void initUi() { rbmListView = (ListView) findViewById(R.id.rbm_listview); rbmOutsideView = (View) findViewById(R.id.rbm_outside_view); // 点击阴影处隐藏 rbmOutsideView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { hideMenu(); } }); // item点击隐藏 rbmListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //回调 if (callback != null) callback.MenuItemClick(menuItems.get(position).id); hideMenu(); } }); } public void setMenuClickCallback(MenuItemCallback callback) { this.callback = callback; } public void setMenuItems(int menu) { parseXml(menu); if (menuItems != null && menuItems.size() > 0) { rbmListView.setAdapter(new Adapter()); } } // 加载图片资源 public void setBackgroundResource(int resource) { rbmListView.setBackgroundResource(resource); } // 显示菜单 public void showMenu() { rbmOutsideView.setVisibility(View.VISIBLE); rbmOutsideView.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.menu_in_from_left)); rbmListView.setVisibility(View.VISIBLE); rbmListView.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.menu_in_from_left)); } // 隐藏菜单 public void hideMenu() { rbmOutsideView.setVisibility(View.GONE); rbmOutsideView.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.menu_out_to_left)); rbmListView.setVisibility(View.GONE); rbmListView.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.menu_out_to_left)); } //隐藏与显示的交替 public void toggleMenu() { if (rbmOutsideView.getVisibility() == View.GONE) { showMenu(); } else { hideMenu(); } } // 读取xml文件 private void parseXml(int menu) { menuItems = new ArrayList<MenuView.RibbonMenuItem>(); try { XmlResourceParser xpp = getResources().getXml(menu); xpp.next(); int eventType = xpp.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { if (eventType == XmlPullParser.START_TAG) { String elemName = xpp.getName(); if (elemName.equals("item")) { // 第一个参数是命名空间,第二个参数是属性名 String text = xpp.getAttributeValue( "http://schemas.android.com/apk/res/android", "title"); String iconId = xpp.getAttributeValue( "http://schemas.android.com/apk/res/android", "icon"); String resId = xpp.getAttributeValue( "http://schemas.android.com/apk/res/android", "id"); System.out.println(text + " " + iconId + " " + resId); RibbonMenuItem item = new RibbonMenuItem(); // id值都是@id所以要去掉@ item.id = Integer.valueOf(resId.replace("@", "")); System.out.println(item.id); item.text = resourceIdToString(text); item.icon = Integer.valueOf(iconId.replace("@", "")); menuItems.add(item); } } eventType = xpp.next(); } } catch (Exception e) { e.printStackTrace(); } } // 判断字符串中是否含有@字符 private String resourceIdToString(String text) { if (!text.contains("@")) { return text; } else { String id = text.replace("@", ""); return getResources().getString(Integer.valueOf(id)); } } //判断当前菜单是否隐藏 public boolean isMenuVisible() { return rbmOutsideView.getVisibility() == View.VISIBLE; } @Override protected void onRestoreInstanceState(Parcelable state) { SavedState ss = (SavedState) state; super.onRestoreInstanceState(ss.getSuperState()); if (ss.bShowMenu) showMenu(); else hideMenu(); } // 保存UI状态 @Override protected Parcelable onSaveInstanceState() { Parcelable superState = super.onSaveInstanceState(); SavedState ss = new SavedState(superState); ss.bShowMenu = isMenuVisible(); return ss; } // 保存界面状态 static class SavedState extends BaseSavedState { boolean bShowMenu; SavedState(Parcelable superState) { super(superState); } private SavedState(Parcel in) { super(in); bShowMenu = (in.readInt() == 1); } @Override public void writeToParcel(Parcel out, int flags) { super.writeToParcel(out, flags); out.writeInt(bShowMenu ? 1 : 0); } public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() { public SavedState createFromParcel(Parcel in) { return new SavedState(in); } public SavedState[] newArray(int size) { return new SavedState[size]; } }; } class RibbonMenuItem { int id; String text; int icon; } private class Adapter extends BaseAdapter { private LayoutInflater inflater; public Adapter() { inflater = LayoutInflater.from(getContext()); } @Override public int getCount() { return menuItems.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { final ViewHolder holder; if (convertView == null || convertView instanceof TextView) { convertView = inflater.inflate( R.layout.activity_menu_item, null); holder = new ViewHolder(); holder.image = (ImageView) convertView .findViewById(R.id.rbm_item_icon); holder.text = (TextView) convertView .findViewById(R.id.rbm_item_text); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.image.setImageResource(menuItems.get(position).icon); holder.text.setText(menuItems.get(position).text); return convertView; } class ViewHolder { TextView text; ImageView image; } } }回调方法:
public interface MenuItemCallback { //点击item时调用 public void MenuItemClick(int itemId); }
主布局:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="点击左上角图标左边的箭头,就能看到效果了!" /> <com.example.testtexiao7.MenuView android:id="@+id/ribbonMenuView1" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout>
menu布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <ListView android:id="@+id/rbm_listview" android:layout_width="@dimen/menu_width" android:layout_height="match_parent" android:background="@color/menu_background" android:visibility="gone" android:cacheColorHint="#00000000"> </ListView> <!-- 阴影 --> <View android:id="@+id/rbm_outside_view" android:layout_width="0dp" android:layout_height="match_parent" android:background="#e0000000" android:layout_weight="1" android:clickable="true" android:visibility="gone" /> </LinearLayout>
item:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <ListView android:id="@+id/rbm_listview" android:layout_width="@dimen/menu_width" android:layout_height="match_parent" android:background="@color/menu_background" android:visibility="gone" android:cacheColorHint="#00000000"> </ListView> <!-- 阴影 --> <View android:id="@+id/rbm_outside_view" android:layout_width="0dp" android:layout_height="match_parent" android:background="#e0000000" android:layout_weight="1" android:clickable="true" android:visibility="gone" /> </LinearLayout>
这是主要的部分代码,其他的文件就先不贴了。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。