【发布时间】:2017-03-29 07:00:46
【问题描述】:
我正在使用可折叠的工具栏和具有 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>
这里也是截图
【问题讨论】:
-
添加 app:layout_behavior="@string/appbar_scrolling_view_behavior" 应用程序崩溃了。
-
错误日志是:>java.lang.RuntimeException:无法启动活动 ComponentInfo{com.chandra.user.newstrailerapp.app/com.chandra.user.newstrailerapp.app.MainActivity}:java。 lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)”,> at com.chandra.user.newstrailerapp.app.MainActivity.onCreate(MainActivity. java:33)
-
@ak sacha 回复我....
-
@ak sacha 但我已经初始化了。签入 MainActicity.java
标签: android android-tabhost android-collapsingtoolbarlayout