簡體   English   中英

ActionBarSherlock setCustomView無法在Android 3.2中運行

[英]ActionBarSherlock setCustomView not working in Android 3.2

我最近升級了我的應用程序以使用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>

試試這個,它在所有設備上運行正常,或者你可以在這里查看演示代碼

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

圖片

在此輸入圖像描述

嘗試這個:

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

以下是我如何使用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>

你試過把兩個孩子的LinearLayout放在一個主要的LinearLayout嗎? 還試着不要重復兒童LinearLayout的id,因為你正在讓你的id系統哭泣。 但我幾乎可以肯定你的問題是你在XML文件中有多個主要的布局。

使用actionbarsherlib在setCustomView下面使用以下代碼。 它也適用於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);

這是我的代碼。我將Sherlock bar庫添加到我的項目中。然后,我在這里用於設置標題上的標題(頂部)。 像這樣..只是嘗試這樣..將目標SDK設置為Android 3.2(版本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");

嘗試這種方法....

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM