我有一个主标签活动,其中我有两个标签,标签位置位于布局下方,我希望标签位置位于操作栏下方,如谷歌播放,当我从一个标签移动到另一个标签时,数据显示来自上一个选项卡,即使我将saveEnabled设置为false。 这是我的代码
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Do the same for the other tabs
intent = new Intent().setClass(this, RunningOrder.class);
spec = tabHost.newTabSpec("runningOrder")
.setIndicator("Running Order", res.getDrawable(R.drawable.running_order))
.setIndicator("Running Order")
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, OrderHistoryTab.class);
spec = tabHost
.newTabSpec("orderHistory")
.setIndicator("Order History",
res.getDrawable(R.drawable.history_icon))
.setContent(intent);
tabHost.addTab(spec);
//set tab which one you want open first time 0 or 1 or 2
tabHost.setCurrentTab(0);
这是我的xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:saveEnabled="false"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:saveEnabled="false"
android:layout_weight="1"/>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:textAlignment="textStart"
android:saveEnabled="false"
android:layout_marginBottom="-4dp"/>
</LinearLayout>
答案 0 :(得分:0)
用于在操作栏交换frameLayout和tabWidget位置下方制作标签位置。
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:textAlignment="textStart"
android:saveEnabled="false"
android:layout_marginBottom="-4dp"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:saveEnabled="false"
android:layout_weight="1"/>
答案 1 :(得分:0)
要实现Google Play,您可以将TabLayout与来自设计支持库的viewpager一起使用。
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/hometoolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/app_action_bar_primary"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways" />
<android.support.design.widget.TabLayout
android:id="@+id/home_tab_layout"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_below="@+id/hometoolbar"
android:background="?attr/colorPrimary"
app:borderWidth="0dp"
app:tabPaddingStart="-1dp"
app:tabPaddingEnd="-1dp"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:tabIndicatorColor="@color/app_color_secondary"
app:tabIndicatorHeight="5dp"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/home_tab_layout"/>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
在您的活动中,您可以编码如下,
TabLayout tabLayout = (TabLayout) findViewById(R.id.home_tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Running Order"));
tabLayout.addTab(tabLayout.newTab().setText("Order History"));
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
您可以通过扩展FragmentStatePagerAdapter来创建Adapter,如下所示
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
RunningOrderFragment tab1 = new RunningOrderFragment();
return tab1;
case 1:
OrderHistoryFragment tab2 = new OrderHistoryFragment();
return tab2;
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
希望这会对你有所帮助。