gpt4 book ai didi

android - Actionbar 选项卡和应用程序标题在同一行

转载 作者:太空狗 更新时间:2023-10-29 16:21:07 25 4
gpt4 key购买 nike

我创建了一个应用程序,其中包含操作栏和两个选项卡。应用程序的选项卡和标题与屏幕截图中给出的位于同一行。我希望标签位于标题下方的一行中并且具有相同的权重。请查看下面的代码并帮助我。谢谢。

package com.example.myproj;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;
import android.content.Context;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity1 extends Activity {
public static Context appContext;
static ArrayList<String> myarray = new ArrayList();
static ArrayList<String> myarray1;
static ArrayList<String> myarray2 = new ArrayList();
static ArrayList<String> myarray21;
static int check1 = 0;
static int check2 = 0;
static int stcount = 0;
static int csicount = 0;
static ActionBar.Tab PlayerTab;
static ActionBar.Tab StationsTab;

/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

appContext = getApplicationContext();

ActionBar actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

PlayerTab = actionbar.newTab().setText("ST");
StationsTab = actionbar.newTab().setText("CSI");
// settab1("Testing");
Fragment PlayerFragment = new AFragment();
Fragment StationsFragment = new BFragment();

PlayerTab.setTabListener(new MyTabsListener(PlayerFragment));
StationsTab.setTabListener(new MyTabsListener(StationsFragment));

actionbar.addTab(PlayerTab);
actionbar.addTab(StationsTab);

}

public static void settab1(String text) {
PlayerTab.setText(text);
}

public static void settab2(String text) {
StationsTab.setText(text);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;

public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(MainActivity1.appContext, "Reselected!",
Toast.LENGTH_LONG).show();
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}

}

enter image description here

最佳答案

我很幸运地使用了以下反射“hack”:

private void forceStackedTabs() {
ActionBar ab = getSupportActionBar();
if ( ab instanceof ActionBarImpl ) {
// Pre-ICS
disableEmbeddedTabs( ab );
} else if ( ab instanceof ActionBarWrapper ) {
// ICS
try {
Field abField = ab.getClass().getDeclaredField( "mActionBar" );
abField.setAccessible( true );
disableEmbeddedTabs( abField.get( ab ) );
} catch (NoSuchFieldException e) {
Log.e( TAG, "Error disabling actionbar embedded", e );
} catch (IllegalArgumentException e) {
Log.e( TAG, "Error disabling actionbar embedded", e );
} catch (IllegalAccessException e) {
Log.e( TAG, "Error disabling actionbar embedded", e );
}
}
}
private void disableEmbeddedTabs(Object ab) {
try {
Method setHasEmbeddedTabsMethod = ab.getClass().getDeclaredMethod("setHasEmbeddedTabs", boolean.class);
setHasEmbeddedTabsMethod.setAccessible(true);
setHasEmbeddedTabsMethod.invoke(ab, false);
} catch (Exception e) {
Log.e( TAG, "Error disabling actionbar embedded", e );
}
}

请注意,我自己并没有想到这一点,只是重写了这个答案中给出的代码:replicate ActionBar Tab(s) with custom view

关于android - Actionbar 选项卡和应用程序标题在同一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14577008/

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