在图像按钮上单击显示溢出菜单

时间:2015-10-23 07:32:15

标签: android android-layout android-fragments

我想在public class FragmentSubCategory extends Fragment{ private View mSubCategoryStatusView; private subCategoryListAdapter subAdapterList; private SelectSubCategoryTask mSubAuthTask = null; private ExpandableListView lstSubCategoryList; String MainCategoryId="-1",MainCategoryName; SubCategoryDetails mainSubCategory; @SuppressLint("NewApi") @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { this.getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); FontUtils.setCustomFont(getActivity().findViewById(R.id.subfragment), getActivity().getAssets()); View view = inflater.inflate(R.layout.activity_fragment_sub,container, false); lstSubCategoryList = (ExpandableListView)view.findViewById(R.id.lstSubCategory); mSubCategoryStatusView = view.findViewById(R.id.sub_category_status); Bundle bundle = getArguments(); try{ MainCategoryName = bundle.getString("MainCategoryName"); }catch(Exception ex){ MainCategoryName=""; } try{ MainCategoryId = bundle.getString("MainCategoryId"); }catch(Exception ex){ MainCategoryId="-1"; } lstSubCategoryList.setOnChildClickListener(new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Intent vendorListIntent=new Intent(getActivity().getApplication(),VendorListByCategoryActivity.class); vendorListIntent.putExtra("typeid", mainSubCategory.masterinfo.get(groupPosition).type.get(childPosition).typeid); vendorListIntent.putExtra("subCategoryId", mainSubCategory.masterinfo.get(groupPosition).subcatid); vendorListIntent.putExtra("mainCategoryId", MainCategoryId); startActivity(vendorListIntent); return false; } }); showSubCategoryProgress(true); mSubAuthTask = new SelectSubCategoryTask(); mSubAuthTask.execute((Void) null); return view; } public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getActivity().getMenuInflater().inflate(R.menu.activity_sub_category, menu); return true; } @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2) private void showSubCategoryProgress(final boolean show) { // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow // for very easy animations. If available, use these APIs to fade-in // the progress spinner. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { int shortAnimTime = getResources().getInteger( android.R.integer.config_shortAnimTime); mSubCategoryStatusView.setVisibility(View.VISIBLE); mSubCategoryStatusView.animate().setDuration(shortAnimTime) .alpha(show ? 1 : 0) .setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { mSubCategoryStatusView.setVisibility(show ? View.VISIBLE : View.GONE); } }); lstSubCategoryList.setVisibility(View.VISIBLE); lstSubCategoryList.animate().setDuration(shortAnimTime) .alpha(show ? 0 : 1) .setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { lstSubCategoryList.setVisibility(show ? View.GONE : View.VISIBLE); } }); } else { // The ViewPropertyAnimator APIs are not available, so simply show // and hide the relevant UI components. mSubCategoryStatusView.setVisibility(show ? View.VISIBLE : View.GONE); lstSubCategoryList.setVisibility(show ? View.GONE : View.VISIBLE); } } public class SelectSubCategoryTask extends AsyncTask<Void, Void, Boolean> { @Override protected Boolean doInBackground(Void... params) { ServerAccess sa=new ServerAccess(); mainSubCategory=sa.GetTypesMasters(Integer.parseInt(MainCategoryId)); return true; } @Override protected void onPostExecute(final Boolean success) { mSubAuthTask = null; if(mainSubCategory==null){ showSubCategoryProgress(false); Toast.makeText(getActivity().getApplicationContext(), getResources().getString(R.string.error_check_network), Toast.LENGTH_LONG).show(); //Message no category found. }else if(mainSubCategory.code.equals("MOB02")){ showSubCategoryProgress(false); Toast.makeText(getActivity().getApplicationContext(), mainSubCategory.msg, Toast.LENGTH_LONG).show(); }else if(mainSubCategory.code.equals("MOB01")){ showSubCategoryProgress(false); subAdapterList=new subCategoryListAdapter(getActivity().getApplicationContext(), mainSubCategory.masterinfo); lstSubCategoryList.setAdapter(subAdapterList); //set adapter }else{ showSubCategoryProgress(false); Toast.makeText(getActivity().getApplicationContext(), getResources().getString(R.string.error_genric_error), Toast.LENGTH_LONG).show(); //No Category message } } @Override protected void onCancelled() { mSubAuthTask = null; showSubCategoryProgress(false); } } public class subCategoryListAdapter extends BaseExpandableListAdapter { private Context myContext; private List<SubCategory> _listDataHeader; // header titles // child data in format of header title, child title public subCategoryListAdapter(Context context, List<SubCategory> listDataHeader) { this.myContext = context; this._listDataHeader = listDataHeader; } @Override public Object getChild(int groupPosition, int childPosititon) { return this._listDataHeader.get(groupPosition).type.get(childPosititon).typename; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final String childText = (String) getChild(groupPosition, childPosition); RelativeLayout layout = new RelativeLayout(myContext); TextView txtListChild = new TextView(myContext); txtListChild.setText(childText); txtListChild.setGravity(Gravity.LEFT); txtListChild.setTextSize(16); txtListChild.setPadding(60, 20, 0, 20); txtListChild.setTextColor(myContext.getResources().getColor(R.color.BlackColor)); layout.addView(txtListChild); return layout; } @Override public int getChildrenCount(int groupPosition) { return this._listDataHeader.get(groupPosition).type.size(); } @Override public Object getGroup(int groupPosition) { return this._listDataHeader.get(groupPosition).subcatname; } @Override public int getGroupCount() { return this._listDataHeader.size(); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { String headerTitle = (String) getGroup(groupPosition); int noOfType= getChildrenCount(groupPosition); RelativeLayout layout = new RelativeLayout(myContext); TextView lblListHeader = new TextView(myContext); lblListHeader.setText(headerTitle.toUpperCase()); lblListHeader.setGravity(Gravity.LEFT); lblListHeader.setTextSize(18); lblListHeader.setPadding(50, 20, 0, 20); lblListHeader.setTextColor(myContext.getResources().getColor(R.color.BlackColor)); layout.addView(lblListHeader); RelativeLayout.LayoutParams noOfTypesLp = new RelativeLayout.LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); noOfTypesLp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); TextView txtNoOfType = new TextView(myContext); txtNoOfType.setText(String.valueOf(noOfType)); txtNoOfType.setTextSize(16); txtNoOfType.setPadding(0, 20, 20, 20); txtNoOfType.setTextColor(myContext.getResources().getColor(R.color.BlackColor)); layout.addView(txtNoOfType,noOfTypesLp); return layout; } @Override public boolean hasStableIds() { return false; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } 点击上显示“溢出”菜单。

菜单/ menu_scrollable_tab.xml

ImageButton

我已禁用默认操作栏并使用布局创建了操作栏。我没有使用默认操作栏或工具栏,因为我需要更多地自定义操作栏。

布局/ actionbar.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    tools:context="com.example.scrollingtab.activity.ScrollableTabsActivity">

    <item android:id="@+id/action_settings" 
        android:title="@string/action_settings"
        android:orderInCategory="100" 
        app:showAsAction="always"
        android:icon="@drawable/info" />

      <item android:id="@+id/action_settings1" 
        android:title="@string/action_settings"
        android:orderInCategory="100" 
        app:showAsAction="never" />

        <item android:id="@+id/action_settings2" 
        android:title="@string/action_settings"
        android:orderInCategory="100" 
        app:showAsAction="never" />
</menu>

根据客户要求,我使用布局设计了操作栏,外观很好。

默认的<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="50dp" > <LinearLayout android:layout_width="fill_parent" android:layout_height="50dp" android:orientation="horizontal" android:background="@color/actionbar_bg" android:weightSum="2"> <ImageButton android:id="@+id/ib_navigation" android:background="@drawable/nv_drawer_24x24" android:layout_width="0dp" android:layout_weight=".3" android:layout_height="50dp"/> <View android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="1.1"/> <ImageButton android:id="@+id/ib_navigation1" android:background="@drawable/info" android:layout_width="0dp" android:layout_weight=".3" android:layout_height="50dp"/> <ImageButton android:id="@+id/ib_navigation2" android:background="@drawable/ic_drawer" android:layout_width="0dp" android:layout_weight=".3" android:layout_height="50dp"/> </LinearLayout> </RelativeLayout> 方法没有填充溢出菜单。现在,我想在点击OnCreateOptionsMenu(Menu menu) ImageButton时填充溢出菜单。我不知道如何实现这一点。

任何人都可以帮我这么做。

由于

1 个答案:

答案 0 :(得分:0)

您需要覆盖创建自己菜单的方法

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.game_menu, menu);
    return true;
}

按钮点击可以调用openOptionsMenu();

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        openOptionsMenu();
    }
});

否则

public myOnClickMethod(View v) {
    openOptionsMenu();
}

有关菜单的详细信息,请参见此处 http://developer.android.com/guide/topics/ui/menus.html

希望这对你有用