使用折叠工具栏和tabhost

时间:2017-03-29 07:00:47

标签: android android-tabhost android-collapsingtoolbarlayout

我正在使用可折叠的工具栏和具有3个选项卡的Tabhost。但不幸的是,Tabhost覆盖了工具栏。我希望Tabhost位于工具栏的正下方。

MainActivity.java

package com.chandra.user.newstrailerapp.app;

import android.app.LocalActivityManager;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TabHost;

public class MainActivity extends AppCompatActivity {

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

    TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
    toolbar = (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    LocalActivityManager mLocalActivityManager = new LocalActivityManager(this, false);
    mLocalActivityManager.dispatchCreate(savedInstanceState);
    tabHost.setup(mLocalActivityManager);



    //RecycleVIew
    RecyclerView recyclerView=(RecyclerView)findViewById(R.id.list);
    recyclerView.setHasFixedSize(true);

    //to use RecycleView, you need a layout manager. default is LinearLayoutManager
    LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(linearLayoutManager);
    YoutubecardAdapter adapter=new YoutubecardAdapter(MainActivity.this);
    recyclerView.setAdapter(adapter);

}
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.chandra.user.newstrailerapp.app.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@color/red"
        android:id="@+id/toolbar"
        app:theme="@style/ThemeOverlay.AppCompat.Dark"
        app:layout_scrollFlags="scroll|enterAlways">

    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

<TabHost
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@android:id/tabhost">

    <LinearLayout
        android:id="@+id/LinearLayout01"
        android:orientation="vertical"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent">
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent">

            <RelativeLayout
                android:id="@+id/tab1"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <android.support.v7.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/list1">

                </android.support.v7.widget.RecyclerView>

            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/tab2"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <android.support.v7.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/list2">

                </android.support.v7.widget.RecyclerView>

            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/tab3"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <android.support.v7.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/list3">

                </android.support.v7.widget.RecyclerView>

            </RelativeLayout>

        </FrameLayout>

    </LinearLayout>

</TabHost>

</android.support.design.widget.CoordinatorLayout>

此处还有截图

Screenshot of MainActivity

1 个答案:

答案 0 :(得分:0)

android:layout_gravity="bottom"添加到AppBarLayout

android:gravity="top"到工具栏