自定义微调器未在ActionBar图标

时间:2015-05-25 07:48:24

标签: android android-actionbar android-arrayadapter android-spinner android-menu

尝试让我的自定义Spinner只使用ImageView(显示图标drawables列表)才能工作。我以为我的自定义适配器代码是正确的,onCreateOptionsMenu()代码是正确的,但没有。共享功能的图标显示在ActionBar中,但是当我触摸它时,Spinner不会下拉它应该的菜单。可能有什么不对?这个目标是独一无二的,因为它不仅仅是一个自定义微调器(很容易找到教程)或只是一个ActionBar图标放置(也很简单),而是将它们组合在一起,这很难找到资源。

enter image description here

PhotoViewerActivity.java

package org.azure_simbiosys.cutecollection.phototab;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;

import org.azure_simbiosys.R;

public class PhotoViewerActivity extends ActionBarActivity
                                implements AdapterView.OnItemSelectedListener{

    private ArrayAdapter spinnerAdapter;
    int[] iconList = {R.drawable.ic_facebook, R.drawable.ic_twitter, R.drawable.ic_sms};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_viewer);

        // Shows the up carat near app icon in ActionBar
        getSupportActionBar().setDisplayUseLogoEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        ImageView photoView = (ImageView)findViewById(R.id.photo_display);

        // gets the single bitmap image from the array of bytes
        Bundle extras = getIntent().getExtras();
        byte[] byteArray = extras.getByteArray("photo");
        Bitmap bm = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

        // sets device orientation based on the image orientation
        if (bm.getWidth() > bm.getHeight()){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        // sets bitmap into your ImageView
        photoView.setImageBitmap(bm);


    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        // Facebook, Twitter, SMS code here

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    private class ShareSpinnerAdapter extends ArrayAdapter {

        // constructor
        public ShareSpinnerAdapter (Context context, int iconResourceId){

            super(context, iconResourceId);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            View itemView = convertView;

            return getCustomView(position, convertView, parent);
        }

        @Override
        public View getDropDownView(int position, View convertView,ViewGroup parent) {

            return getCustomView(position, convertView, parent);
        }

        public View getCustomView(int position, View convertView, ViewGroup parent)
        {

            LayoutInflater inflater=getLayoutInflater();
            View row=inflater.inflate(R.layout.share_spinner_row, parent, false);

            ImageView icon=(ImageView)row.findViewById(R.id.spinner_icon);
            icon.setImageResource(iconList[position]);

            return row;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_share, menu);

        Spinner shareSpinner = (Spinner) menu.findItem(R.id.action_social_share).getActionView();

        spinnerAdapter = new ShareSpinnerAdapter(this, R.layout.share_spinner_row);
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        shareSpinner.setAdapter(spinnerAdapter);

        shareSpinner.setBackground(getResources().getDrawable(R.drawable.ic_action_social_share));

        // add listener
        shareSpinner.setOnItemSelectedListener(this);

        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Makes the UP caret go back to the previous fragment MakeCuteHome
        switch (item.getItemId()) {
            case android.R.id.home:
                android.app.FragmentManager fm = getFragmentManager();
                fm.popBackStack();
                finish();
                return true;
            case R.id.action_social_share:


                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

menu_share.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <!-- Share, should appear as action button -->
    <item android:id="@+id/action_social_share"
        android:icon="@drawable/ic_action_social_share"
        android:title="Share"
        android:actionLayout="@layout/action_share"
        app:showAsAction="always"
        app:actionViewClass="android.widget.Spinner"/>

</menu>

action_share.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">

    <Spinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/share_spinner"
        android:layout_gravity="center_horizontal"
        android:spinnerMode="dropdown"/>
</LinearLayout>

share_spinner_row

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

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/spinner_icon"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

如果您希望操作栏上的共享图标作为微调器工作...然后在menu_share.xml文件中创建项目标记的子项为..

 <item android:id="@+id/menu"
      android:icon="@drawable/menu"
      android:title="menu"

      android:showAsAction="always" >


      <menu>

    <item
        android:id="@+id/Login"
        android:title="Login"
        android:icon="@drawable/contact" />
    <item
        android:id="@+id/share"
        android:title="Share"
        android:icon="@drawable/share" />
     <item
        android:id="@+id/like"
        android:title="Like"
        android:icon="@drawable/favourite" />
</menu>

    </item>