2

通常最好的学习研究工作的例子和星星点点 黑客在一起,直到我明白了一切是如何工作的。我没有用ActionBarsABS太多的经验,但我发现的原生ActionBar一个工作演示。我在这里找到:https://github.com/johannilsson/android-actionbar转换本地操作栏即可操作栏福尔摩斯

我得到了这个演示和ABS library启动和运行在日食。我现在的问题是我该如何将其转换为ABS动作条或者使用ABS重新创建等价物?(仅仅是一个简单的起动器ABS动作条,我可以使用它来连接不同活动的几个物品。)

这里是操作栏演示代码:

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

回答

5

ActionBarSherlock是API 14 & 15行动起来吧这就是Android的一部分的API完成反向移植。

如果您是图书馆新手,我建议您先试着学习如何使用native action bar。你熟悉使用它,只有后,切换到使用ABS。

交换机将是令人难以置信的容易,因为它主要是由这三样东西:

  • com.actionbarsherlock.appandroid.view改变从android.app几个进口(例如,ActionBarMenuItem)到com.actionbarsherlock.view
  • 更改主题从Theme.HoloTheme.Sherlock(或.Light.Light.DarkActionBar
  • 改变你的活动从SherlockActivity而不是Activity延伸。
  • 切换呼叫getActionBar()getSupportActionBar()

易如馅饼!

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