使用tablayout时缺少操作栏

csbfibhn  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(213)

我已经从支持库中实现了tablayout,但是它覆盖了我需要的actionbar。

这是一个活动,它设置和充气的标签和加载标签的碎片。

public class TabsActivity extends android.support.v4.app.FragmentActivity {

    FloatingActionButton fab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tabs);

        setTitle("Fuel Logger");

        ActionBar a = getActionBar();

        ViewPager vp = (ViewPager) findViewById(R.id.viewpager);
        vp.setAdapter(new FragmentPagerAdapter(
                getSupportFragmentManager(), TabsActivity.this
        ));

        TabLayout tabLayout = (TabLayout) findViewById(R.id.fuel_sliding_tabs);
        tabLayout.setupWithViewPager(vp);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.fuel_menu, menu);
        MenuItem refresh = menu.findItem(R.id.action_settings);
        refresh.setEnabled(true);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        switch (id){
            case R.id.action_settings:

                break;
            case R.id.action_favorite:

                return true;

            default:

        }
            return super.onOptionsItemSelected(item);
        }

    }

xml只是视图页面和选项卡布局

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

    <android.support.design.widget.TabLayout
        style="@style/FuelTabLayout"
        android:id="@+id/fuel_sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="scrollable"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1.27"
        android:background="@android:color/white"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/fuel_sliding_tabs" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:src="@android:drawable/ic_menu_set_as"
        app:layout_anchorGravity="bottom|right|end"
        app:backgroundTint="@color/colorAccent"
        android:layout_gravity="right"
        android:cropToPadding="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="20dp"
        android:layout_marginEnd="20dp"
        android:layout_marginBottom="20dp" />

</RelativeLayout>

以及一个片段代码的示例,对于每个片段类来说或多或少是相同的

Public class SummaryFragment extends android.support.v4.app.Fragment {

public static final String ARG_PAGE = "SUMM_PAGE";
private int mTab;

public static SummaryFragment newInstance(int page){
    Bundle args = new Bundle();
    args.putInt(ARG_PAGE, page);
    SummaryFragment frag = new SummaryFragment();
    frag.setArguments(args);
    return frag;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mTab = getArguments().getInt(ARG_PAGE);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,     Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_tab_summary, null);

    return view;
}
vwkv1x7d

vwkv1x7d1#

首先添加工具栏,并使用linearlayout witch具有水平方向和设置样式的noactionbar,希望对您有所帮助

相关问题