创建FragmentTabHost造成一些麻烦 [英] Creating FragmentTabHost causing some trouble

查看:230
本文介绍了创建FragmentTabHost造成一些麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我开发一个应用程序,到现在为止,我是用普通的标签布局有两个活动。现在有人告诉我,这是更好的做usning FragmentTabHost。所以,我一直在阅读一些文档,这是我想出迄今:

MainActivity_Fragment:

 公共类MainActivity_Fragment扩展片段{
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
    捆绑savedInstanceState){
    //充气的布局该片段
    返回inflater.inflate(R.layout.activity_main,集装箱,FALSE);
    }}

RecordedLibrary_Fragment:

 公共类RecordedLibrary_Fragment扩展片段{
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
    捆绑savedInstanceState){
    //充气的布局该片段
    返回inflater.inflate(R.layout.activity_recorded_library,集装箱,FALSE);
    }
}

TabLayout活动:

 公共类TabLayout扩展FragmentActivity {// GLOBAL变量///////////////
私人FragmentTabHost mTab​​Host;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_tab_layout);    mTabHost =(FragmentTabHost)findViewById(android.R.id.tabhost);
    mTabHost.setup(这一点,getSupportFragmentManager(),r.id ....); //< - 在这里的问题    mTabHost.addTab(mTabHost.newTabSpec(记录)。setIndicator(记录),
            MainActivity_Fragment.class,NULL);    mTabHost.addTab(mTabHost.newTabSpec(库)。setIndicator(库),
            RecordedLibrary_Fragment.class,NULL);
    }
}

现在,这是我在哪里卡住了。但是也有一些没有在文档解释了一些事情。所以,我有两个问题。

这行的文档:

  mTab​​Host.setup(这一点,getSupportFragmentManager(),R.id.realtabcontent);

在哪里他们得到R.id.realtabcontent?

和未解释的另一件事;标签活动的XML应该怎么看?

我目前的标签布局的xml:

 < TabHost的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@机器人:ID / tabhost
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>
<的LinearLayout
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>
    < TabWidget
        机器人:ID =@机器人:ID /标签
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT/>
    <的FrameLayout
        机器人:ID =@机器人:ID / tabcontent
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT/>
< / LinearLayout中>

正如你所看到的,标签布局XML仍然是从旧的标签布局。我是否需要将其调整到FragmentTabLayout还是好这样?


解决方案

  

现在有人告诉我,这是更好的做usning FragmentTabHost。


好吧,我的没有的<一个href=\"http://stackoverflow.com/questions/18133136/how-to-execute-oncreate-method-when-activity-in-tablayout-is-selected#comment26555020_18133136\">say FragmentTabHost 可能是最流行的3,虽然它是最接近你的当前code。


  

在哪里他们得到R.id.realtabcontent?


这是在布局小部件的ID。特别是它会在 R.layout.fragment_tabs ,它的文档忽略显现。

幸运的是,这似乎是从支撑包装的样品,并且该布局可以发现<一href=\"https://github.com/android/platform_development/blob/2d52182dfac91014c2975a1bb1afd99a3b14b4e9/samples/Support4Demos/res/layout/fragment_tabs.xml\"相对=nofollow>此处,用在写这篇文章的时间的定义:

 &LT; android.support.v4.app.FragmentTabHost
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@机器人:ID / tabhost
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent&GT;    &LT;的LinearLayout
        机器人:方向=垂直
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent&GT;        &LT; TabWidget
            机器人:ID =@机器人:ID /标签
            机器人:方向=横向
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =0/&GT;        &LT;的FrameLayout
            机器人:ID =@机器人:ID / tabcontent
            机器人:layout_width =0dp
            机器人:layout_height =0dp
            机器人:layout_weight =0/&GT;        &LT;的FrameLayout
            机器人:ID =@ + ID / realtabcontent
            机器人:layout_width =match_parent
            机器人:layout_height =0dp
            机器人:layout_weight =1/&GT;    &LT; / LinearLayout中&GT;
&LT; /android.support.v4.app.FragmentTabHost>


  

和未解释的另一件事;标签活动的XML应该怎么看?


见上文。


  

我是否需要将其调整到FragmentTabLayout还是好这样?


好了,如果不出意外,你将需要更换 TabHost FragmentTabHost

不过,坦率地说, FragmentTabHost 是更糟,文档明智的,比我对你刚​​才的问题发表意见的,我记得。

So I'm developing an app and until now, I was using ordinary tab layout with two activities. Now someone told me that it's much better to do it usning FragmentTabHost. So I've been reading some docs and this is what I came up with so far:

MainActivity_Fragment:

public class MainActivity_Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.activity_main, container, false);
    }

}

RecordedLibrary_Fragment:

public class RecordedLibrary_Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.activity_recorded_library, container, false);
    }
}

TabLayout activity:

public class TabLayout extends FragmentActivity {

//GLOBAL VARIABLES///////////////
private FragmentTabHost mTabHost;

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

    mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), r.id....); // <-- problem here

    mTabHost.addTab(mTabHost.newTabSpec("Recording").setIndicator("Recording"),
            MainActivity_Fragment.class, null);

    mTabHost.addTab(mTabHost.newTabSpec("Library").setIndicator("Library"),
            RecordedLibrary_Fragment.class, null);
    }
}

Now this is where I am stuck. There are a few things that aren't explained in the docs. So I have two questions.

This line in the docs:

mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

Where did they get R.id.realtabcontent ?

And another thing that isn't explained; how should the xml of the tab activity look?

My current tab layout xml:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</LinearLayout>

As you can see, the tab layout xml is still from the old tab layout. Do I need to adjust it to FragmentTabLayout or is it good this way?

解决方案

Now someone told me that it's much better to do it usning FragmentTabHost.

Well, I did say "FragmentTabHost is probably the least popular of the 3, though it's closest to your current code."

Where did they get R.id.realtabcontent ?

That's the ID of a widget in a layout. Specifically it would be in R.layout.fragment_tabs, which the documentation neglects to show.

Fortunately, this appears to be from the support package samples, and that layout can be found here, with the definition at the time of this writing as:

<android.support.v4.app.FragmentTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TabWidget
            android:id="@android:id/tabs"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"/>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"/>

        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

    </LinearLayout>
</android.support.v4.app.FragmentTabHost>

And another thing that isn't explained; how should the xml of the tab activity look?

See above.

Do I need to adjust it to FragmentTabLayout or is it good this way?

Well, if nothing else, you will need to replace TabHost with FragmentTabHost.

But, frankly, FragmentTabHost is worse off, documentation-wise, than I was remembering when I commented on your earlier question.

这篇关于创建FragmentTabHost造成一些麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆