繁体   English   中英

带有活动的Android标签

[英]Android tabs with activities

我正在尝试构建一个底部具有选项卡的应用程序,按下该按钮时,用户可以导航到不同的视图,并且在这些视图上按下按钮时,会触发新的活动,但底部的选项卡菜单会保留在那里(用户可以继续导航到其他视图(如果需要)。 我该如何以不推荐的方式进行操作?

您可以使用片段,并且只能使用一个活动,

该活动将在底部具有固定的选项卡,并具有用于放置可替换片段的容器。

每当您要导航到其他视图时,只需替换活动容器中的片段,底部选项卡将保持原样。

这是一个简单的演示,显示了选项卡的用法:

public class MainActivity extends AppCompatActivity {

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

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        ViewPager viewPager = (ViewPager) findViewById(R.id.content_pager);
        Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);

        FragmentPager fragmentPager = new FragmentPager(getSupportFragmentManager());

        for(int i = 0; i < 3; i++){

            Bundle args = new Bundle();
            args.putString(SimpleFragment.FRAG_KEY, "Fragment "+ i);
            fragmentPager.addFragment(SimpleFragment.newInstance(args));
        }

        viewPager.setAdapter(fragmentPager);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    }


    public class FragmentPager extends FragmentPagerAdapter{

        List<Fragment> mFragments;

        public FragmentPager(FragmentManager fm) {
            super(fm);
            mFragments = new ArrayList<>();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return "Fragment "+ position;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        public void addFragment(Fragment fragment){
            mFragments.add(fragment);
        }
    }
}

使用此布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.divshark.bottomtabs.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/content_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="?actionBarSize">

    </android.support.v4.view.ViewPager>
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            style="@style/MyCustomTabLayout"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize">
        </android.support.design.widget.TabLayout>
    </android.support.v7.widget.Toolbar>
</RelativeLayout>

使用SimpleFragment:

public class SimpleFragment extends Fragment {

    public static final String FRAG_KEY = "FragmentKey";


    public static SimpleFragment newInstance(Bundle args){
        SimpleFragment fragment = new SimpleFragment();

        if(args != null){
            fragment.setArguments(args);
        }

        return fragment;
    }

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

        AppCompatTextView mTextView = (AppCompatTextView) view.findViewById(R.id.tv_simple);

        if(getArguments() != null){

            String fragKey = getArguments().getString(FRAG_KEY, "No Key passed");

            mTextView.setText(fragKey);
        }

        return view;
    }
}

布局:

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

    <android.support.v7.widget.AppCompatTextView
        android:id="@+id/tv_simple"
        tools:text="@string/app_name"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</FrameLayout>

和风格:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>


    <style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
        <item name="tabIndicatorColor">?attr/colorAccent</item>
        <item name="tabIndicatorHeight">2dp</item>
        <item name="tabPaddingStart">12dp</item>
        <item name="tabPaddingEnd">12dp</item>
        <item name="tabBackground">?attr/selectableItemBackground</item>
        <item name="tabTextAppearance">@style/MyCustomTabTextAppearance</item>
        <item name="tabSelectedTextColor">?android:textColorPrimary</item>
    </style>
    <style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
        <item name="android:textSize">14sp</item>
        <item name="android:textColor">?android:textColorSecondary</item>
        <item name="textAllCaps">true</item>
    </style>

</resources>

和build.gradle依赖项:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.0'
    compile 'com.android.support:design:23.2.0'
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM