让actionbar隐藏的时候,不重新布局

0.总结

actionbar遇到slidingmenu后overlay模式无效,未解决

1.未使用slidingmenu,将actionbar隐藏

隐藏使用代码

ActionBar actionBar = getSupportActionBar();
actionBar.hide();

隐藏的时候回activity 会 re-layout 。所以要设置成overlay模式,即定义activity的

 windowActionBarOverlay to true

另外还有一个属性,在布局文件中可能会用到

android:layout_marginTop="?android:attr/actionBarSize" 

参考:

http://developer.android.com/guide/topics/ui/actionbar.html

http://developer.android.com/training/basics/actionbar/overlaying.html

2.未使用slidingmenu,将actionbarsherlock隐藏

下面是官方的例子:

参考:https://github.com/JakeWharton/ActionBarSherlock/blob/master/actionbarsherlock-samples/demos/src/com/actionbarsherlock/sample/demos/Overlay.java

        requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.overlay);

        //Load partially transparent black background
        getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.ab_bg_black));

3.slidingmenu中关于overlay的例子

(使用的是actionbarsherlock)

并且实现点击展示,2s后小时的功能

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		int pos = 0;
		if (getIntent().getExtras() != null) {
			pos = getIntent().getExtras().getInt("pos");
		}
		
		String[] birds = getResources().getStringArray(R.array.birds);
		TypedArray imgs = getResources().obtainTypedArray(R.array.birds_img);
		int resId = imgs.getResourceId(pos, -1);
		
		setTitle(birds[pos]);
		getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
		ColorDrawable color = new ColorDrawable(Color.BLACK);
		color.setAlpha(128);
		getSupportActionBar().setBackgroundDrawable(color);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		mHandler = new Handler();
		
		ImageView imageView = new ImageView(this);
		imageView.setScaleType(ScaleType.CENTER_INSIDE);
		imageView.setImageResource(resId);
		imageView.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				getSupportActionBar().show();
				hideActionBarDelayed(mHandler);
			}
		});
		setContentView(imageView);
		this.getWindow().setBackgroundDrawableResource(android.R.color.black);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		getSupportActionBar().show();
		hideActionBarDelayed(mHandler);
	}

	
	private void hideActionBarDelayed(Handler handler) {
		handler.postDelayed(new Runnable() {
			public void run() {
				getSupportActionBar().hide();
			}
		}, 2000);
	}


4.碰到slidingmenu,overlay模式会失效(未找到解决方案)

猜你喜欢

转载自20142014.iteye.com/blog/2011541
今日推荐