Android 使用ActionBarSherlock选项卡导航按ID显示FrameLayout内的片段时出现问题
我很难弄清楚这件事。。我查看了SampleFragments ABS应用程序,它使用TabHost和一个自定义侦听器来实现这一点。。这是我唯一的选择还是我能让它工作我是如何让它工作的。。(我想使用默认的TabNav,因为它在横向模式下覆盖在ActionBar上以节省屏幕空间 主要活动:Android 使用ActionBarSherlock选项卡导航按ID显示FrameLayout内的片段时出现问题,android,android-fragments,actionbarsherlock,android-tabs,Android,Android Fragments,Actionbarsherlock,Android Tabs,我很难弄清楚这件事。。我查看了SampleFragments ABS应用程序,它使用TabHost和一个自定义侦听器来实现这一点。。这是我唯一的选择还是我能让它工作我是如何让它工作的。。(我想使用默认的TabNav,因为它在横向模式下覆盖在ActionBar上以节省屏幕空间 主要活动: public class MainActivity extends SherlockFragmentActivity implements ActionBar.TabListener { /** Called w
public class MainActivity extends SherlockFragmentActivity implements ActionBar.TabListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//create Tab Nav
ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
bar.addTab(bar.newTab()
.setText("Tab1")
.setTabListener(this));
bar.addTab(bar.newTab()
.setText("Tab2")
.setTabListener(this));
bar.addTab(bar.newTab()
.setText("Tab3")
.setTabListener(this));
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction transaction) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction transaction) {
FrameLayout frame = (FrameLayout) findViewById(R.id.tabcontent);
if (tab.getText() == "Tab2")
{
frame.setBackgroundColor(0xFF00FF00);
}
if (tab.getText() == "Tab1")
{
frame.setBackgroundColor(0xFFFFFFFF);
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction transaction) {
}
}
支持与片段活动
public class TestSupport extends SherlockFragmentActivity{
public static Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.buttonOMG);
FragmentManager fm = getSupportFragmentManager();
// Create the list fragment and add it as our sole content.
if (fm.findFragmentById(android.R.id.tabcontent) == null) {
MapsFragment list = new MapsFragment();
fm.beginTransaction().add(android.R.id.tabcontent, list).commit();
}
}
public static class TestFragment extends SherlockListFragment {
@Override public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//setHasOptionsMenu(true);
btn.setText("LOL");
}
}
}
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#CCC">
<FrameLayout
android:id="@+id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="@+id/buttonOMG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</FrameLayout>
</LinearLayout>
我只是用按钮文本的变化作为一个指标,看看它是否正确实现
我遇到的问题是在OnTableSelected类中。我如何调用片段来执行并使其显示在tabcontent布局上?我的片段和支持是否正确编码到我只需要调用它就可以工作的位置?我被难住了
谢谢!你有没有想过?好问题,我也想看看答案!你有没有想过?好问题,我也想看看答案!