2013-11-26 72 views
1

我有一個片段,我想在操作欄中加載自定義視圖,所以我做了以下操作。CustomView in ActionBar not loading

在活動形式:

public class FormActivity extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_form); 

     try { 
      FragmentManager fragmentManager = getSupportFragmentManager(); 
      FragmentTransaction fragmentTransaction = fragmentManager 
       .beginTransaction(); 

      fragmentTransaction.replace(R.id.content_frame, new FormFragment()).addToBackStack(null).commit(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 

在片段本身:

public class FormFragment extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     View actionBarButtons = inflater.inflate(R.layout.form_custom_actionbar, 
       new LinearLayout(getActivity()), false); 
     View cancelActionView = actionBarButtons.findViewById(R.id.action_cancel); 
     cancelActionView.setOnClickListener(this); 
     View doneActionView = actionBarButtons.findViewById(R.id.action_done); 
     doneActionView.setOnClickListener(this); 

     ActionBar ab = ((ActionBarActivity) getActivity()).getSupportActionBar(); 
     ab.setCustomView(actionBarButtons); 

     return inflater.inflate(R.layout.fragment_form, null); 
    } 
... 

的CustomActionBar資源佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:divider="?android:attr/dividerVertical" 
    android:dividerPadding="12dip" 
    android:showDividers="middle"> 

    <!-- id must match corresponding menu item id --> 
    <LinearLayout 
     android:id="@+id/action_cancel" 
     style="@style/FormCustomActionButton"> 

     <ImageView 
      android:src="@drawable/ic_action_remove" 
      style="@style/FormCustomActionButtonImage" /> 
     <TextView 
      android:text="@string/discard_label" 
      style="@style/FormCustomActionButtonText" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

    </LinearLayout> 

    <!-- id must match corresponding menu item id --> 
    <LinearLayout 
     android:id="@+id/action_done" 
     style="@style/FormCustomActionButton"> 

     <ImageView 
      android:src="@drawable/abc_ic_cab_done_holo_light" 
      style="@style/FormCustomActionButtonImage" /> 
     <TextView 
      android:text="@string/save_label" 
      style="@style/FormCustomActionButtonText" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

    </LinearLayout> 


</LinearLayout> 

片段加載罰款,但我不明白頂部的自定義操作欄按鈕。我錯過了什麼?

+1

http://stackoverflow.com/questions/17521745/manually-inflating-custom-view-yields-different -layouts換動作條定製-VIE。檢查這是否有幫助 – Raghunandan

+0

是的,這有幫助!我添加了'actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);''和'actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);' 現在它工作! – spacebiker

+1

很高興幫助。真棒 – Raghunandan

回答