2016-02-25 48 views
0

我试图建立在底部,按下该按钮时,用户可以导航到不同的视图和标签应用时按下按钮,在这些视图的新活性被激发,但底部的选项卡菜单住宿在那里(如果需要,用户可以继续导航到其他视图)。我怎样才能以非弃用的方式来做到这一点?Android的卡口与活动

+0

你会想要做的是使用1活动,并加载多个片段。对于酒吧的底部我会建议使用工具栏和包装是 –

+0

非过时的方式里面的标签?它有什么不赞成的方式来做到这一点?我不明白这个问题。你为什么不在每个布局中包含标签?无论如何,你应该使用'片段'。这正是他们的目的。 –

+0

是的,我可以使用片段来呈现不同的意见。但是当我按下按钮时,我想调用一个新的活动并替换该片段,然后如果我按回我想要再次返回到该片段。这是我的实际问题。 另外我如何使用工具栏并将其附加在底部?我不能让它在大屏幕上工作... –

回答

2

您可以使用片段和只有一个活动,

活动将有固定在底部和可更换碎片的容器标签。

每当要导航到不同的视图,只需更换活动的容器中的片段和底部卡将保持原样。

+0

但是,当我在一个片段按下一个按钮,我想火在它的地方活动。我应该发射一个新的片段吗? –

+0

@CapBaracudas:“我应该发射一个新的片段吗?” - 是的。如果你想要一个持久性的UI(例如你的标签栏),切换到新的活动并不是正确的解决方案。 – CommonsWare

0

这里是示出标签的使用一个简单的演示:使用此布局

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' 
} 
+0

如果我使用ViewPager,我怎样才能跟踪我的片段,并更新它?我在这里找到了解决方案(1),但它似乎应该是一种更简单或更正式的方式? 1.http://tamsler.blogspot.co.uk/2011/11/android-viewpager-and-fragments-part-ii.html –

+0

@CapBaracudas您可以使用适配器得到针对当前项目的引用:'SimpleFragment片段=(SimpleFragment)fragmentPager.getItem(viewPager.getCurrentItem());' –