android - 将 native 操作栏转换为操作栏 Sherlock

标签 android android-intent android-actionbar actionbarsherlock

通常通过研究工作示例和零碎拼凑来学习得最好 直到我明白一切是如何运作的。我对 ActionBarsABS 没有太多经验,但我找到了 native ActionBar 的工作演示。我在这里找到:https://github.com/johannilsson/android-actionbar

我在 eclipse 中启动并运行了这个演示和 ABS 库。我现在的问题是我如何将其转换为 ABS 操作栏或使用 ABS 重新创建等效项?(只是一个我可以熟悉的简单的入门 ABS 操作栏,其中有几个链接到不同 Activity 的项目。)

这是操作栏演示代码:

package com.markupartist.android.actionbar.example;

import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.Action;
import com.markupartist.android.widget.ActionBar.IntentAction;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class HomeActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
        //actionBar.setHomeAction(new IntentAction(this, createIntent(this), R.drawable.ic_title_home_demo));
        actionBar.setTitle("Home");

        final Action shareAction = new IntentAction(this, createShareIntent(), R.drawable.ic_title_share_default);
        actionBar.addAction(shareAction);
        final Action otherAction = new IntentAction(this, new Intent(this, OtherActivity.class), R.drawable.ic_title_export_default);
        actionBar.addAction(otherAction);

        Button startProgress = (Button) findViewById(R.id.start_progress);
        startProgress.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                actionBar.setProgressBarVisibility(View.VISIBLE);
            }
        });

        Button stopProgress = (Button) findViewById(R.id.stop_progress);
        stopProgress.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                actionBar.setProgressBarVisibility(View.GONE);
            }
        });

        Button removeActions = (Button) findViewById(R.id.remove_actions);
        removeActions.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                actionBar.removeAllActions();
            }
        });

        Button addAction = (Button) findViewById(R.id.add_action);
        addAction.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                actionBar.addAction(new Action() {
                    @Override
                    public void performAction(View view) {
                        Toast.makeText(HomeActivity.this, "Added action.", Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public int getDrawable() {
                        return R.drawable.ic_title_share_default;
                    }
                });
            }
        });

        Button removeAction = (Button) findViewById(R.id.remove_action);
        removeAction.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                int actionCount = actionBar.getActionCount();
                actionBar.removeActionAt(actionCount - 1);
                Toast.makeText(HomeActivity.this, "Removed action." , Toast.LENGTH_SHORT).show();
            }
        });

        Button removeShareAction = (Button) findViewById(R.id.remove_share_action);
        removeShareAction.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                actionBar.removeAction(shareAction);
            }
        });
    }

    public static Intent createIntent(Context context) {
        Intent i = new Intent(context, HomeActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        return i;
    }

    private Intent createShareIntent() {
        final Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, "Shared from the ActionBar widget.");
        return Intent.createChooser(intent, "Share");
    }
}

最佳答案

ActionBarSherlock 是 API 14 和 15 操作栏的 API 完整向后移植,它是 Android 的一部分。

如果您是该库的新手,我建议您首先尝试学习如何使用 native action bar 。只有在熟悉使用后,才改用 ABS。

切换将非常容易,因为它主要由以下三部分组成:

  • 将一些导入(例如 ActionBarMenuItem)从 android.app 更改为 com.actionbarsherlock.appandroid.viewcom.actionbarsherlock.view
  • 将主题从 Theme.Holo 更改为 Theme.Sherlock(或 .Light.Light.DarkActionBar >)
  • 将您的 Activity 更改为从 SherlockActivity 扩展,而不是从 Activity 扩展。
  • 将对 getActionBar() 的调用切换为 getSupportActionBar()

简单易行!

此外,应该注意的是,您的演示不是 native 操作栏,而是第三方库,该库在内置到操作系统之前模拟操作栏。

关于android - 将 native 操作栏转换为操作栏 Sherlock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14493178/

相关文章:

android - 带有浅色应用程序主题的深色工具栏

java - Android启动 Activity 不工作

android - 如何使用 AppCompat 库在 ActionBar 中显示 NavigationDrawer 图标

android - 如何设置Android工具栏高度?

android - 在 2 个应用程序之间连续处理 Intent

android - 键盘未隐藏在 ActionBar 选项卡更改上

java - 如何创建无卡全屏 Android Wear 应用

android - 在 Android 中实现 SeekBar Listener 接口(interface)时出现空指针异常

java - 重新加载 fragment

android - 在另一个 Intent 中发送 Intent