0

試圖讓我的自定義微調只有一個ImageView(顯示圖標可繪製的列表)工作。我認爲我的自定義適配器代碼是正確的,並且onCreateOptionsMenu()代碼正確,但沒有。共享功能的圖標顯示在ActionBar中,但當我觸摸它時,Spinner不會下拉它應該顯示的菜單。什麼可能是錯的?這個目標是獨一無二的,因爲它不僅僅是一個自定義的微調器(很容易找到教程),或者只是一個ActionBar圖標放置(也很容易),而是將它們組合在一起,這很難找到資源。自定義微調不在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> 

回答

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> 
+0

這只是一個例子...試試這個 –

+0

謝謝,我試過,但父母是紅色的。它說元素項必須聲明。 – Azurespot

+0

你有沒有把父項也放在菜單標籤中? –

相關問題