2013-11-14 84 views
0

我試圖使用ActionBarSherlock庫將自定義佈局添加到ActionBar。 我發現儘管看起來沒有錯誤,但不顯示佈局。將自定義佈局添加到ActionBarSherlock失敗

 View customView=LayoutInflater.from(this).inflate(R.layout.layout_actionbar_top,null); 
    ImageButton ab_camera=(ImageButton) customView.findViewById(R.id.ab_camera); 
    ImageButton ab_gallery=(ImageButton)customView.findViewById(R.id.ab_gallery); 
    ab_camera.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      startActivity(new Intent(getBaseContext(),CameraActivity.class)); 
     }}); 

    ab_gallery.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent intent=new Intent(); 
      intent.setAction(Intent.ACTION_GET_CONTENT); 
      intent.setType("image/*"); 
      intent.addCategory(Intent.CATEGORY_OPENABLE); 
      startActivityForResult(intent, IMAGE_REQUEST_CODE); 

     }}); 
    LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,Gravity.RIGHT | Gravity.CENTER_VERTICAL); 
    getSupportActionBar().setCustomView(customView,params); 

該活動將在ActionBar中沒有自定義佈局的情況下加載。

這是自定義的佈局,LinearLayout是水平:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<ImageButton android:id="@+id/ab_camera" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@android:drawable/ic_menu_camera"/> 

<ImageButton android:id="@+id/ab_gallery" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@android:drawable/ic_menu_gallery" 
      /> 
</LinearLayout> 

回答

0

我未能設置一個標誌,使ActionBarSherlock顯示我customView爲true。

getSupportActionBar().setDisplayShowCustomEnabled(true); 
相關問題