我正在尝试使用底部的标签构建一个应用程序,按下时用户可以导航到不同的视图,当在这些视图上按下按钮时会触发新的活动但是底部标签菜单保留在那里(并且如果用户愿意,用户可以继续导航到其他视图)。我怎么能以非弃用的方式做到这一点?
答案 0 :(得分:2)
您可以使用片段,只能使用一个活动
活动底部有固定标签,可替换片段有容器。
每当您想要导航到不同的视图时,只需替换活动容器中的片段,底部的标签将保持原样。
答案 1 :(得分:0)
这是一个简单的演示,显示了Tabs的使用:
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'
}