android - ActionBar 中的 CustomView 未加载

标签 android android-fragments

我有一个 fragment ,我想从中加载操作栏中的自定义 View ,所以我执行以下操作。

在表单 Activity 中:

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

    }

在 fragment 本身中:

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>

fragment 加载正常,但我没有在顶部获得自定义操作栏按钮。我错过了什么?

最佳答案

将评论转化为答案

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM)

同时检查此链接

Manually inflating custom view yields different layouts for ActionBar custom view

关于android - ActionBar 中的 CustomView 未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20217927/

相关文章:

android - 在 fragmentTransaction.replace() 方法参数中放入什么 id?

android - 为什么 fragment 类应该公开?

android - 样式 appcompat-v7 工具栏菜单背景

Android 如何创建一个可以运行超过 8 小时的计时器应用程序

android - Fragment 中的可扩展 ListView

java - 如何根据ViewPager中当前的ImageView更新父Activity中的TextView

java - 如何使用固定宽高比 View 膨胀扩展 View

android - 为安卓手机制作定制主题

java - 谷歌地图最佳路径

android - MVP:管理 Activity 和 fragment 的最佳实践