我在Android中使用TabLayout时遇到问题。我正在使用AppCompat库,因为我的min SDK是10.问题是如果TabLayout在首次创建活动时具有GONE可见性,那么当我将可见性设置为VISIBLE后,选项卡报告和标签指示符将丢失。
这是我的MainActivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* Called when we press the button.
*/
public void openTabActivity(View view) {
Intent intent = new Intent(this, TabActivity.class);
startActivity(intent);
}
}
TabActivity是这样的:
public class TabActivity extends FragmentActivity {
MyPagerAdapter mMyPagerAdapter;
ViewPager mViewPager;
TabLayout mTabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
// ViewPager and its adapters use support library
// fragments, so use getSupportFragmentManager.
mMyPagerAdapter =
new MyPagerAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.myViewPager);
mViewPager.setAdapter(mMyPagerAdapter);
// Link the TabLayout with the ViewPager.
mTabLayout = (TabLayout) findViewById(R.id.myTab);
mTabLayout.setupWithViewPager(mViewPager);
// If I set visibility GONE it doesn't show titles
// when I set it to VISIBLE again.
// If I remove this, it works fine.
mTabLayout.setVisibility(View.GONE);
}
/**
* If the tab is visible it turn it gone, if it's gone it turn it
* visible.
* @param view
*/
public void toggleTab(View view) {
Log.d(this.getClass().toString(), "ShowTab()");
if (mTabLayout.getVisibility() == View.VISIBLE) {
Log.d(this.getClass().toString(), "Turning GONE");
mTabLayout.setVisibility(View.GONE);
} else {
Log.d(this.getClass().toString(), "Turning VISIBLE");
mTabLayout.setVisibility(View.VISIBLE);
}
}
}
页面适配器:
public class MyPagerAdapter extends FragmentStatePagerAdapter {
final int PAGE_COUNT = 3;
private String tabTitles[] = new String[]{"Tab 1", "Tab 2", "Tab 3"};
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment fragment = new MyFragment();
return fragment;
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
}
片段:
public class MyFragment extends Fragment {
public static MyFragment newInstance() {
MyFragment fragment = new MyFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment, container, false);
return view;
}
}
布局也非常简单:
activity_main.xml中
<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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="200dp"
android:onClick="openTabActivity"
android:textColor="#55F"
android:text="Press to go to Tabs"/>
</RelativeLayout>
activity_tab.xml
<LinearLayout 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"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".TabActivity">
<android.support.design.widget.TabLayout
android:id="@+id/myTab"
style="@style/AppTheme.Tab.NavigationTab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/lightPrimaryColor"/>
<android.support.v4.view.ViewPager
android:id="@+id/myViewPager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"/>
</LinearLayout>
my_fragment.xml
<FrameLayout 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=".MyFragment">
<Button
android:layout_width="200dp"
android:layout_height="200dp"
android:onClick="toggleTab"
android:text="Press"/>
</FrameLayout>
如果我在TabActivity.onCreate中将可见性设置为GONE,则会失败。如果它在TabActivity.onCreate中是可见的,它可以工作。
我尝试使用.invalidate()但它不起作用。
有人能帮助我吗?
提前感谢您的帮助。
答案 0 :(得分:7)
确认。这是库 com.android.support:design:22.2.1 中的一个错误。如果我使用 com.android.support:design:22.2.0 ,它的效果非常好。它将在未来的库版本中得到解决。
答案 1 :(得分:0)
当我没有设置<div class="div" id="1">1</div>
<div class="div" id="2">2</div>
<div class="div" id="3">3</div>
<div class="div" id="4">4</div>
<div class="div" id="5">5</div>
<div class="div" id="6">6</div>
<div id="numberdiv"></div>
和tabTextColor
样式属性时,不确定是否会出现相同的问题,但类似的情况发生在我身上。
从tabSelectedTextColor
升级到com.android.support:support-v13:23.1.1
后中断,使用以下格式解决:
22.2.0