【问题标题】:ActionBarSherlock setCustomView not working in Android 3.2ActionBarSherlock setCustomView 在 Android 3.2 中不起作用
【发布时间】:2012-07-24 22:24:10
【问题描述】:

我最近升级了我的应用程序以使用 ActionBarSherlock 4.1。由于在 Honeycomb 上运行应用程序的升级用户在操作栏上设置自定义视图时会由于空指针异常而强制关闭。

我向操作栏添加了一个包含两个微调器的自定义视图,这适用于 Android 4.0 和 4.1,但不适用于 3.2。

            bar.setCustomView(R.layout.custom_action_bar);// spinners
            actionBarViewHolder= new CustomActionBarViewHolder();
            actionBarViewHolder.categorySpinner = (Spinner) findViewById(R.id.actionbar_catergory);
            actionBarViewHolder.sortBySpinner = (Spinner) findViewById(R.id.actionbar_sortby);

在 Android 3.2 上,尚未在 4.0 和 4.1 上找到微调器,它们的视图已找到并且一切运行顺利。

我没有在 3.0 模拟器上尝试过该应用程序,但我认为问题仍然存在。

任何想法可能是什么问题?

<LinearLayout android:id="@+id/linearLayout1"
    android:layout_width="wrap_content" android:layout_height="match_parent"
    android:gravity="center_vertical|center_horizontal"
    android:layout_weight="0.5">

    <TextView android:id="@+id/textView1" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Category:" />

    <Spinner android:id="@+id/actionbar_catergory"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:entries="@array/actionbar_spinner_catergory"
        android:background="@drawable/spinner_background" />
</LinearLayout>


<LinearLayout android:id="@+id/linearLayout1"
    android:layout_width="wrap_content" android:layout_height="match_parent"
    android:gravity="center_vertical|center_horizontal"
    android:layout_weight="0.5">

    <TextView android:id="@+id/textView2" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Sort By:" />

    <Spinner android:id="@+id/actionbar_sortby"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:entries="@array/actionbar_spinner_sortby" android:background="@drawable/spinner_background" />

</LinearLayout>

【问题讨论】:

    标签: android android-3.0-honeycomb android-actionbar android-4.0-ice-cream-sandwich actionbarsherlock


    【解决方案1】:

    试试这个,它在所有设备上都可以正常工作,或者您可以查看演示代码here

    getSupportActionBar().setCustomView(R.layout.actionbar_top); // load your layout
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    

    图片

    【讨论】:

    • 链接中现在没有任何内容
    【解决方案2】:

    试试这个:

    final View view = inflater.inflate(R.layout.custom_action_bar, null);
    bar.setCustomView(view);// spinners
    actionBarViewHolder= new CustomActionBarViewHolder();
    actionBarViewHolder.categorySpinner = (Spinner) view.findViewById(R.id.actionbar_catergory);
    actionBarViewHolder.sortBySpinner = (Spinner) view.findViewById(R.id.actionbar_sortby);
    

    【讨论】:

    • 谢谢。我稍后会试一试。
    • 很遗憾。这没有用。使用充气机时,Honeycomb 上的微调器仍然为空。
    • 视图已创建并存在,因此 Spinner 不是该视图的子视图。潜在原因:1. 错误的 ID,2. 微调器不是视图的子视图 3. 在错误的视图上调用 findViewById
    • custom_action_bar 包含两个包含微调器的子线性布局。我已经添加了 xml 布局。
    【解决方案3】:

    这是我如何使用 actiobBarSherlock 创建自定义操作栏的代码

     private void createCustomActionBar() {
    
        List<SiteLink> links = new ArrayList<SiteLink>();
        links.add(...)  
        LinksAdapter linkAdapter = new LinksAdapter(this, R.layout.external_link, links);
    
        View customNav = LayoutInflater.from(this).inflate(R.layout.custom_show_action_bar, null);
        IcsSpinner spinner = (IcsSpinner)customNav.findViewById(R.id.spinner);
        spinner.setAdapter(linkAdapter);
    
    
        ImageView refresh = (ImageView) customNav.findViewById(R.id.refresh);
        refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ...
            }
        });
    
        ImageView settings = (ImageView) customNav.findViewById(R.id.settings);
        settings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ...
            }
        });
    
        getSupportActionBar().setCustomView(customNav, new ActionBar.LayoutParams(Gravity.RIGHT));
        getSupportActionBar().setDisplayShowCustomEnabled(true);
    
    }
    

    适配器

    private static class LinksAdapter extends ArrayAdapter<SiteLink> {
    
        private List<SiteLink> strings;
        private Context context;
    
        private LinksAdapter(Context context, int textViewResourceId, List<SiteLink> objects) {
            super(context, textViewResourceId, objects);
            this.strings = objects;
            this.context = context;
        }
    
        @Override
        public int getCount() {
            if (strings == null) return 0;
            return strings.size();
        }
    
        @Override
        public SiteLink getItem(int position) {
            return super.getItem(position);
        }
    
    
        // return views of drop down items
        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
    
            final SiteLink siteLink = strings.get(position);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // at 0 position show only icon
    
            TextView site = (TextView) inflater.inflate(R.layout.external_link, null);
            site.setText(siteLink.getName());
    
            site.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(siteLink.getUrl()));
                    context.startActivity(i);
                }
            });
            return site;
    
    
        }
    
    
        // return header view of drop down
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            return inflater.inflate(R.layout.icon, null);
        }
    }
    

    布局

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                   android:gravity="right"
            >
    
    
        <com.actionbarsherlock.internal.widget.IcsSpinner
            android:id="@+id/spinner"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:paddingRight="20dp"
            android:layout_gravity="center"
                />
    
         <ImageView  android:layout_width="fill_parent"
                     android:layout_height="fill_parent"
                     android:src="@drawable/ic_navigation_refresh"
                     android:paddingRight="20dp"
                     android:paddingLeft="10dp"
                     android:layout_gravity="center"
                     android:background="@drawable/action_buttons_background"
                     android:id="@+id/refresh"/>
    
    
        <ImageView  android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:src="@drawable/ic_action_settings"
                    android:paddingRight="20dp"
                    android:background="@drawable/action_buttons_background"
                    android:layout_gravity="center"
                    android:id="@+id/settings"/>
    
    </LinearLayout>
    

    【讨论】:

    • 不喜欢使用 View customNav = LayoutInflater.from(this).inflate(R.layout.custom_show_action_bar, null);
    • 可以使用非静态LayoutInflater初始化。例如 LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflanter.inflate...
    • 这也行不通。 Honeycomb 好像找不到要充气的布局?
    【解决方案4】:

    您是否尝试过将您的两个孩子LinearLayout 放在主要的LinearLayout 中?也尽量不要重复孩子LinearLayout的id,因为你让你的id系统哭了。但我几乎可以肯定您的问题是您的 XML 文件中有多个主要布局。

    【讨论】:

      【解决方案5】:

      使用下面的代码 setCustomView 使用 actionbarsherlib。 它也适用于 android 3.2 版本。

          getSupportActionBar().setDisplayShowCustomEnabled(true);
          View view = getLayoutInflater().inflate(R.layout.custom_view, null);
          Button mybutton = (Button)view.findViewById(R.id.button1);            
          mybutton.setOnClickListener(new OnClickListener()
          {
                  @Override
                  public void onClick(View v)
                  {
                  /** Your click actions here. */
                  }
          });
          getSupportActionBar().setCustomView(view);
      

      【讨论】:

        【解决方案6】:

        这是我的代码。在这里我将 Sherlock 条库添加到我的项目中。然后,我在这里用于在标题(顶部)上设置我的标题。像这样.. 试试这个吧.. 将目标 SDK 设置为 Android 3.2(Version 14) 或更高版本..

                    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
            getSupportActionBar().setCustomView(R.layout.header_sherlock_xmllayout);
            header_tvleft = (TextView) findViewById(R.id.header_tvleft);
            header_tvleft.setText("Back");
        

        试试这个方法....

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-21
          • 1970-01-01
          • 1970-01-01
          • 2014-08-25
          相关资源
          最近更新 更多