gpt4 book ai didi

java - ConfigChanges 期间 ActionBar 的导航模式变化

转载 作者:行者123 更新时间:2023-11-30 03:27:12 28 4
gpt4 key购买 nike

我正在使用 actionbarsherlock 的 ActionBar,第一次以横向或水平方向运行应用程序时,选项卡以 Tabs Mode 显示。将方向更改为纵向后,导航模式仍在 Tabs 中。第二次将方向更改为 horizo​​ntal 时,选项卡显示为 List 并且仅显示父项,其余项隐藏。我听说这是 Android 的一个错误,如果选项卡太多无法适应屏幕,系统会自动将导航模式更改为 list。但问题是我第一次加载应用程序时它甚至水平显示为 tabs 模式。顺便说一下,我创建了 100 个标签页

此图像处于水平 View 中。从上数第一张图片是我将手机方向更改为纵向然后返回横向后的结果。
enter image description here


我的 list 文件中有这段代码,

android:configChanges="orientation|screenSize"

主要 Activity

 public void onConfigurationChanged(Configuration newConfig) {
ActionBar ab = getSupportActionBar();
ab.setDisplayShowTitleEnabled(false);

if(ab.getNavigationMode() != ActionBar.NAVIGATION_MODE_TABS){
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

}
super.onConfigurationChanged(newConfig);
}

只是为了确保我也把代码放在 setContentView

附近的某个地方
        final ActionBar ab = getSupportActionBar();
ab.setDisplayShowTitleEnabled(false);
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

更新:

我处理 onConfigurationChanged 的原因是因为我遇到了一个空指针异常

逻辑猫

08-05 15:36:18.486: E/AndroidRuntime(30044): FATAL EXCEPTION: main
08-05 15:36:18.486: E/AndroidRuntime(30044): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.actionbarsherlock.sample.styled/com.actionbarsherlock.sample.styled.MainActivity}: java.lang.NullPointerException
08-05 15:36:18.486: E/AndroidRuntime(30044): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-05 15:36:18.486: E/AndroidRuntime(30044): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-05 15:36:18.486: E/AndroidRuntime(30044): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3512)
08-05 15:36:18.486: E/AndroidRuntime(30044): at android.app.ActivityThread.access$700(ActivityThread.java:130)
08-05 15:36:18.486: E/AndroidRuntime(30044): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
08-05 15:36:18.486: E/AndroidRuntime(30044): at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 15:36:18.486: E/AndroidRuntime(30044): at android.os.Looper.loop(Looper.java:137)
08-05 15:36:18.486: E/AndroidRuntime(30044): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-05 15:36:18.486: E/AndroidRuntime(30044): at java.lang.reflect.Method.invokeNative(Native Method)
08-05 15:36:18.486: E/AndroidRuntime(30044): at java.lang.reflect.Method.invoke(Method.java:511)
08-05 15:36:18.486: E/AndroidRuntime(30044): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-05 15:36:18.486: E/AndroidRuntime(30044): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-05 15:36:18.486: E/AndroidRuntime(30044): at dalvik.system.NativeStart.main(Native Method)
08-05 15:36:18.486: E/AndroidRuntime(30044): Caused by: java.lang.NullPointerException
08-05 15:36:18.486: E/AndroidRuntime(30044): at com.nineoldandroids.animation.PropertyValuesHolder.setupSetterAndGetter(PropertyValuesHolder.java:504)
08-05 15:36:18.486: E/AndroidRuntime(30044): at com.nineoldandroids.animation.ObjectAnimator.initAnimation(ObjectAnimator.java:410)
08-05 15:36:18.486: E/AndroidRuntime(30044): at com.nineoldandroids.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:538)
08-05 15:36:18.486: E/AndroidRuntime(30044): at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:928)
08-05 15:36:18.486: E/AndroidRuntime(30044): at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:951)
08-05 15:36:18.486: E/AndroidRuntime(30044): at com.nineoldandroids.animation.ObjectAnimator.start(ObjectAnimator.java:385)
08-05 15:36:18.486: E/AndroidRuntime(30044): at com.actionbarsherlock.sample.styled.MainActivity.rotateLeftFrag(MainActivity.java:218)
08-05 15:36:18.486: E/AndroidRuntime(30044): at com.actionbarsherlock.sample.styled.MainActivity.onTabSelected(MainActivity.java:256)
08-05 15:36:18.486: E/AndroidRuntime(30044): at com.actionbarsherlock.internal.app.ActionBarWrapper$TabWrapper.onTabSelected(ActionBarWrapper.java:356)
08-05 15:36:18.486: E/AndroidRuntime(30044): at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:570)
08-05 15:36:18.486: E/AndroidRuntime(30044): at com.android.internal.app.ActionBarImpl.setSelectedNavigationItem(ActionBarImpl.java:356)
08-05 15:36:18.486: E/AndroidRuntime(30044): at com.android.internal.app.ActionBarImpl.setNavigationMode(ActionBarImpl.java:1158)
08-05 15:36:18.486: E/AndroidRuntime(30044): at com.actionbarsherlock.internal.app.ActionBarWrapper.setNavigationMode(ActionBarWrapper.java:216)
08-05 15:36:18.486: E/AndroidRuntime(30044): at com.actionbarsherlock.sample.styled.MainActivity.showTabsNav(MainActivity.java:244)
08-05 15:36:18.486: E/AndroidRuntime(30044): at com.actionbarsherlock.sample.styled.MainActivity.onCreate(MainActivity.java:82)
08-05 15:36:18.486: E/AndroidRuntime(30044): at android.app.Activity.performCreate(Activity.java:5008)
08-05 15:36:18.486: E/AndroidRuntime(30044): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-05 15:36:18.486: E/AndroidRuntime(30044): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-05 15:36:18.486: E/AndroidRuntime(30044): ... 12 more

最佳答案

这是 android 中选项卡的默认行为。如果在水平/纵向方向上没有更多空间供选项卡使用,那么它将转换为列表。

现在,如果您仍然希望所有选项卡不在列表中,那么我建议您查看 ViewPager 的示例之后,您应该将该 FragmentActivity 添加到您的 SherlockFragmentActivity 中。只需检查下面的示例代码,我在做什么,这将帮助您拥有任何方向的所有选项卡。

public class TestSampleTabDefault extends SherlockActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}
@SuppressLint("NewApi")
public static class SampleTabsDefault extends FragmentActivity implements ServiceConnection {
private static final String[] CONTENT = new String[] { "Albums", "Artists", "Songs", "Playlist","Genre","Recently Played","Easy Transfer" };
Handler mHandler;
//Runnable mRunnable;
private ServiceToken mToken;
private IRemoteService mIRemoteService;
public static ViewPager pager;
public static int index = 0;
public static android.app.ActionBar mActionBar;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_tabs);

//getSupportActionBar().setHomeButtonEnabled(true);
//ActionBar bar = getSupportActionBar();
//bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

FragmentPagerAdapter adapter = new GoogleMusicAdapter(getSupportFragmentManager());

pager = (ViewPager)findViewById(R.id.pager);
pager.setAdapter(adapter);

/*getSupportActionBar().setTitle(name);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);*/

TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);
indicator.setViewPager(pager);

index = getIntent().getExtras().getInt("index");
if(index >0){
indicator.setCurrentItem(index);
}

}

class GoogleMusicAdapter extends FragmentPagerAdapter {
private final FragmentManager mFragmentManager;
private Fragment mFragmentAtPos0;
public GoogleMusicAdapter(FragmentManager fm) {
super(fm);
mFragmentManager = fm;
}
@Override
public Fragment getItem(int position) {
// return TestFragment.newInstance(CONTENT[position % CONTENT.length]);
Fragment fm = null;

if(position==0){// LOAD ALBUMS
fm = ArtistList.ArtistListFragment.newInstance();
return fm;

}else if(position==1){// LOAD ARTISTS
//Toast.makeText(getApplicationContext(), "Select Album", Toast.LENGTH_SHORT).show();
fm = ArtistList.ArtistListFragment.newInstance();
return fm;
}else if(position==2){// LOAD SONGS
fm = SongsList.SongsListFragment.newInstance();
return fm;
}else if(position==3){ // PLaylist
fm = PlaylistList.PlayListFragment.newInstance();
return fm;
}else if(position==4){ // Genre List
fm = GenreList.GenreListFragment.newInstance();
return fm;
}else if(position==5){ // Recent List
fm = RecentAddedList.RecentListFragment.newInstance();
return fm;
}else{
fm = TestFragment.newInstance(CONTENT[position]);
}
return fm;


}

@Override
public CharSequence getPageTitle(int position) {
return CONTENT[position % CONTENT.length].toUpperCase();
}

@Override
public int getCount() {
return CONTENT.length;
}

@Override
public int getItemPosition(Object object)
{
if (object instanceof AlbumListFragment && mFragmentAtPos0 instanceof TestFragment)
return POSITION_NONE;
return POSITION_UNCHANGED;
}

}

}
}

希望对您有所帮助。一切顺利。如果您在这方面遇到任何困难,请告诉我。

关于java - ConfigChanges 期间 ActionBar 的导航模式变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18052142/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com