自定义微调没有出现在动作条图标下拉出现在、自定义、图标、动作

2023-09-12 03:24:14 作者:港台男模

试图让我的自定义微调只有一个ImageView的(显示的图标可绘制列表)工作。我想我有自定义适配器code正确, onCreateOptionsMenu() code正确的,但没有。在动作条的共享功能的图标显示,但是当我触摸它的微调并不下拉列表中选择菜单它应该。可能是什么问题?这个目标是独一无二的,因为它不仅是一个自定义的微调(很容易找到的教程),或者只是一个动作条图标的位置(也容易),而是结合他们两个,这是很难找到的资源。

PhotoViewerActivity.java

 包org.azure_simbiosys.cutecollection.phototab;

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

进口org.azure_simbiosys.R;

公共类PhotoViewerActivity扩展ActionBarActivity
                                实现AdapterView.OnItemSelectedListener {

    私人ArrayAdapter spinnerAdapter;
    INT [] iconList = {R.drawable.ic_facebook,R.drawable.ic_twitter,R.drawable.ic_sms};

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_photo_viewer);

        //显示在附近的动作条的应用程序图标向上克拉
        getSupportActionBar()setDisplayUseLogoEnabled(假)。
        getSupportActionBar()setDisplayHomeAsUpEnabled(真)。

        ImageView的PhotoView中=(ImageView的)findViewById(R.id.photo_display);

        //从字节数组获取单个位图图像
        捆绑额外= getIntent()getExtras()。
        字节[]的字节数组= extras.getByteArray(图片);
        位图BM = BitmapFactory.de codeByteArray(字节数组,0,byteArray.length);

        //套设备方向基于图像方向
        如果(bm.getWidth()> bm.getHeight()){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } 其他
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        //将位图到您的ImageView
        photoView.setImageBitmap(BM);


    }

    @覆盖
    公共无效onItemSelected(适配器视图<>母公司视图中查看,INT位置,长的id){

        //的Facebook,微博,短信code在这里

    }

    @覆盖
    公共无效onNothingSelected(适配器视图<>母公司){

    }

    私有类ShareSpinnerAdapter扩展ArrayAdapter {

        //构造
        公共ShareSpinnerAdapter(上下文的背景下,INT iconResourceId){

            超(背景下,ic​​onResourceId);
        }

        @覆盖
        公共查看getView(INT位置,查看convertView,ViewGroup中父){
            查看ItemView控件= convertView;

            返回getCustomView(位置,convertView,父母);
        }

        @覆盖
        公共查看getDropDownView(INT位置,查看convertView,ViewGroup中父){

            返回getCustomView(位置,convertView,父母);
        }

        公共查看getCustomView(INT位置,查看convertView,父母的ViewGroup)
        {

            LayoutInflater充气= getLayoutInflater();
            查看排= inflater.inflate(R.layout.share_spinner_row,父母,假);

            ImageView的图标=(ImageView的)row.findViewById(R.id.spinner_icon);
            icon.setImageResource(iconList [位置]);

            返回行;
        }
    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        //充气操作栏中使用的菜单项
        MenuInflater充气= getMenuInflater();
        inflater.inflate(R.menu.menu_share,菜单);

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

        spinnerAdapter =新ShareSpinnerAdapter(这一点,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));

        //添加监听器
        shareSpinner.setOnItemSelectedListener(本);

        返回super.onCreateOptionsMenu(菜单);
    }


    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //使向上插入符回到previous片段MakeCuteHome
        开关(item.getItemId()){
            案例android.R.id.home:
                android.app.FragmentManager FM = getFragmentManager();
                fm.popBackStack();
                完();
                返回true;
            案例R.id.action_social_share:


                返回true;
            默认:
                返回super.onOptionsItemSelected(项目);
        }
    }
}
 

menu_share.xml

 < XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto>

    <  - !共享,应该显示为操作按钮 - >
    <项目机器人:ID =@ + ID / action_social_share
        机器人:图标=@可绘制/ ic_action_social_share
        机器人:标题=分享
        机器人:actionLayout =@布局/ action_share
        应用程序:showAsAction =总是
        应用程序:actionViewClass =android.widget.Spinner/>

< /菜单>
 

action_share.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直机器人:layout_width =match_parent
    机器人:layout_height =match_parent>

    <微调
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:ID =@ + ID / share_spinner
        机器人:layout_gravity =center_horizo​​ntal
        机器人:spinnerMode =下拉列表中/>
< / LinearLayout中>
 
自定义显示图标和通知

share_spinner_row

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直机器人:layout_width =match_parent
    机器人:layout_height =match_parent>

    < ImageView的
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:ID =@ + ID / spinner_icon
        机器人:layout_gravity =center_horizo​​ntal/>
< / LinearLayout中>
 

解决方案

如果你想在动作条上的工作作为一个微调共享图标...然后创建项目标签的孩子在menu_share.xml文件。

 <项目机器人:ID =@ + ID /菜单
      机器人:图标=@可绘制/菜单
      机器人:标题=菜单

      机器人:showAsAction =总是>


      <菜单>

    <项目
        机器人:ID =@ + ID /登录
        机器人:标题=登陆
        机器人:图标=@可绘制/接触/>
    <项目
        机器人:ID =@ + ID /股
        机器人:标题=分享
        机器人:图标=@可绘制/股/>
     <项目
        机器人:ID =@ + ID /像
        机器人:标题=像
        机器人:图标=@可绘制/最爱/>
< /菜单>

    < /项目>
 

Trying to get my custom Spinner with only an ImageView (to show a list of icon drawables) to work. I thought I had the custom adapter code correct, and the onCreateOptionsMenu() code correct, but no. The icon for the sharing feature shows in the ActionBar, but when I touch it, the Spinner does not dropdown the menu it should. What could be wrong? This goal is unique, because it is not just a custom spinner (easy to find tutorial on) or just an ActionBar icon placement (also easy), but rather combining them both, which is hard to find resources on.

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>

解决方案

If you want share icon on action bar work as a spinner... then create child of item tag in menu_share.xml file as..

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