android 文本 被清空,android – 当可见性从GONE变为VISIBLE时,TabLayout文本消失

我在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中

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">

android:layout_width="match_parent"

android:layout_height="200dp"

android:onClick="openTabActivity"

android:textColor="#55F"

android:text="Press to go to Tabs"/>

activity_tab.xml

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:id="@+id/myTab"

style="@style/AppTheme.Tab.NavigationTab"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="@color/lightPrimaryColor"/>

android:id="@+id/myViewPager"

android:layout_width="match_parent"

android:layout_height="0px"

android:layout_weight="1"/>

my_fragment.xml

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MyFragment">

android:layout_width="200dp"

android:layout_height="200dp"

android:onClick="toggleTab"

android:text="Press"/>

如果我在TabActivity.onCreate中设置可见性GONE,则会失败.如果它在TabActivity.onCreate中是可见的,它可以工作.

我试过使用.invalidate()但它不起作用.

有谁能够帮我?

在此先感谢您的帮助.

解决方法:

证实.这是库com.android.support:design:22.2.1中的一个错误.如果我使用com.android.support:design:22.2.0它可以很好地工作.它将在未来的库版本中得到解决.

标签:visibility,android,android-tablayout

来源: https://codeday.me/bug/20190611/1222032.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值