Android不显示标签 [英] Android does not show tabs

查看:125
本文介绍了Android不显示标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在更改家庭活动以包括标签.我使用 https://github.com/saulmm/CoordinatorExamples 作为来源.出于未知原因,我在AppBarLayout中看不到选项卡.我可以看到Fragment的内容,但是标签头根本没有显示.我使用的是appcompat-v7:23.3.0.

I am in a process of changing home activity to include tabs. I use https://github.com/saulmm/CoordinatorExamples as a source. For unknown reason I do not see the tabs in my AppBarLayout. I can see the Fragment content but tab headers are not displayed at all. I use appcompat-v7:23.3.0.

布局缩短:

<android.support.design.widget.CoordinatorLayout
  <android.support.design.widget.AppBarLayout
    <android.support.design.widget.CollapsingToolbarLayout
        <ImageView ..
        <android.support.v7.widget.Toolbar ..
    </android.support.design.widget.CollapsingToolbarLayout>
    <android.support.design.widget.TabLayout
        android:id="@+id/main.tabs"
        android:layout_width="fill_parent"
        android:layout_height="?attr/actionBarSize"
        app:tabSelectedTextColor="?android:attr/textColorPrimaryInverse"
        app:tabIndicatorColor="?android:attr/textColorPrimaryInverse"
        app:tabIndicatorHeight="4dp" />
  </android.support.design.widget.AppBarLayout>

  <android.support.v4.view.ViewPager
    android:id="@+id/dashboard.viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    />

活动:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);

    CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.main_collapsing);
    collapsingToolbarLayout.setTitle(getString(R.string.app_name));

    ViewPager viewPager  = (ViewPager) findViewById(R.id.dashboard_viewpager);
    viewPager.setAdapter(new TabsAdapter(getSupportFragmentManager()));
    TabLayout tabLayout = (TabLayout) findViewById(R.id.main_tabs);
    tabLayout.setupWithViewPager(viewPager);
}

class TabsAdapter extends FragmentPagerAdapter {
    public TabsAdapter(FragmentManager fm) {
        super(fm);
    }

    public int getCount() {
        return 1;
    }

    public Fragment getItem(int i) {
        switch(i) {
            case 0: return DashboardHomeFragment.newInstance();
        }
        return null;
    }

    public CharSequence getPageTitle(int position) {
        return "Home";
    }
}

片段:

public class DashboardHomeFragment extends Fragment implements View.OnClickListener {
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_dashboard_home, container, false);
}

public void onActivityCreated(@Nullable Bundle state) {
    log.debug("onActivityCreated()");
    super.onActivityCreated(state);
}

及其布局:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Hry"
    android:textAppearance="@style/TextAppearance.Header"
    android:paddingRight="8dp"
    android:paddingLeft="8dp"
    style="@style/TextComponent.ItemRow"/>

<TextView
    android:id="@+id/main.button.puzzle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Najdi výsledek"
    android:textAppearance="@style/TextAppearance.Item"
    android:drawableRight="@drawable/ic_arrow_forward_24dp"
    style="@style/TextComponent.ItemRow.Selectable"/>

还有第二个问题,但是我将其发布到与此问题相关的单独问题中.

There is a second problem but I will post it in separate question linked to this question.

更新:

这是由CoordinatorLayout配置错误引起的.我改变了:

It was caused by CoordinatorLayout misconfiguration. I changed:

android:layout_height="150dp"

android:layout_height="wrap_content"

选项卡突然出现.

推荐答案

根据Android的准则和材料设计,使用coordinatorLayout是正确的.

According to the guidelines of Android and the material design, it is correct to use the coordinatorLayout.

appBarLayout的最大长度应为256dp,在内部我们可以找到工具栏和所需的视图.

The appBarLayout should be max 256dp and inside we find the toolbar and the views that you need.

如果要折叠的视图插入collapsingToolbarLayout.

If you want the views collapsing insert into collapsingToolbarLayout.

如果您希望工具栏可扩展,将被插入collapsingToolbarLayout内.

If you want the toolbar expandable will be inserted inside the collapsingToolbarLayout.

tabLayout通常插入到appBarLayout中,但不折叠到ToolbarLayout中.

The tabLayout often is insert into appBarLayout but out collapsingToolbarLayout.

视图高度的总和等于appBarLayout的高度(或对appBarLayout使用wrap_content).

The sum of views height is equals at appBarLayout height (or use wrap_content for appBarLayout).

这是带有tabLayout的可扩展工具栏的示例,在这种情况下,appBarLayout具有固定的高度,但是您可以使用wrap_content.

This is an example of expandable toolbar with tabLayout, in this case appBarLayout has a fixed height but you can use wrap_content.

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="256dp"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">


        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="202dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="@color/your_color"
            android:fitsSystemWindows="true">

            <--Your views-->

            <android.support.v7.widget.Toolbar
               android:id="@+id/toolbar"
               android:layout_width="match_parent"
               android:layout_height="?attr/actionBarSize"
               android:gravity="top"
               app:titleMarginTop="13dp"
               android:minHeight="?attr/actionBarSize"
               app:popupTheme="@style/AppTheme.PopupOverlay"
               app:layout_collapseMode="pin"
            />
        </android.support.design.widget.CollapsingToolbarLayout>

       <android.support.design.widget.TabLayout
               android:id="@+id/tabs"
               android:layout_width="match_parent"
               android:layout_height="?attr/actionBarSize"
               android:layout_gravity="bottom"
               android:background="@color/your_color"
               app:tabSelectedTextColor="@color/your_color"
               app:tabTextColor="@color/your_color"
               app:tabIndicatorColor="@color/your_color"
               app:tabMode="fixed"
               app:tabGravity="fill"
           />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />
</android.support.design.widget.CoordinatorLayout>

这篇关于Android不显示标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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