下面試這個library的網站,在這裡我要簡單的介紹這個library的使用方法
http://actionbarsherlock.com/
why use actionbarsherlock
由於內建的actionbar限制很多,有許多特別的UI都無法達成,所以就找到這個library,他使得客製化變得非常方便。
如何使用actionbar
要使用這個library首先必須先將 project reference 引進,這在Android 開發 (十三) ViewPager 使用方法 有介紹如何引用,接著下面是簡單的範例
public class MainActivity extends SherlockActivity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn).setOnClickListener(this);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
}
@Override
public void onClick(View view) {
if(view.getId()==R.id.btn){
LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.mylayout, null);
((TextView)v.findViewById(R.id.txt)).setText("123");
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
v.setLayoutParams(params);
v.setBackgroundColor(Color.YELLOW);
this.getSupportActionBar().setCustomView(v);
}
}
}
在onCreate時設定 displayoptions為 DISPLAY_SHOW_CUSTOM ,
接著在button click時將 actionbar設定成任意UI
這樣就完成了
附上 sample code
附註
如果希望能夠在actionbar上面有progressbar的功能
只需要使用
setSupportProgress(Window.PROGRESS_END);
setSupportProgressBarIndeterminateVisibility(true);
要使progressbar消失則使用
setSupportProgressBarIndeterminateVisibility(false);
沒有留言:
張貼留言