2016-06-24 13 views
1

最初にツールバーがあり、リサイクルビューを追加しましたが、ツールバーは表示されなくなりました。私はツールバーを表示するためにすべてを試みたが、私はいつも失敗した。どのようにツールバーを戻すことができるのか教えていただけますか?前もって感謝します。ここで Androidでツールバーが表示されない

public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener{ 

    private Toolbar mToolbar; 
    private FragmentDrawer drawerFragment; 

    private RecyclerView recyclerView; 
    private IsAdapter isAdapter; 
    private List<Is> isList; 


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

     mToolbar = (Toolbar) findViewById(R.id.toolbar); 

     setSupportActionBar(mToolbar); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 

     recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 

     isList = new ArrayList<>(); 
     isAdapter = new IsAdapter(this, isList); 


     RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 1); 
     recyclerView.setLayoutManager(mLayoutManager); 
     recyclerView.addItemDecoration(new GridSpacingItemDecoration(1, dpToPx(10), true)); 
     recyclerView.setItemAnimator(new DefaultItemAnimator()); 
     recyclerView.setAdapter(isAdapter); 

     prepareIsler(); 

     drawerFragment = (FragmentDrawer) 
       getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer); 
     drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar); 
     drawerFragment.setDrawerListener(this); 



     getSupportActionBar().setTitle("TTT"); 
     // display the first navigation drawer view on app launch 
     displayView(0); 


    } 

がレイアウトコードです: はここに私のJavaコードである

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


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

     <LinearLayout 
      android:id="@+id/container_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:local="http://schemas.android.com/apk/res-auto" 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:minHeight="?attr/actionBarSize" 
       android:background="?attr/colorPrimary" 
       android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
       /> 

     <!-- <include 
       android:id="@+id/toolbar" 
       layout="@layout/toolbar" /> --> 
     </LinearLayout> 

     <FrameLayout 
      android:id="@+id/container_body" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 
    </LinearLayout> 

    <fragment 
     android:id="@+id/fragment_navigation_drawer" 
     android:name="com.example.neozeka1.dts2.FragmentDrawer" 
     android:layout_width="@dimen/nav_drawer_width" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     app:layout="@layout/fragment_navigation_drawer" 
     tools:layout="@layout/fragment_navigation_drawer" /> 

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/white" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     tools:context="info.androidhive.cardview.MainActivity" 
     tools:showIn="@layout/activity_main"> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/recycler_view" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:clipToPadding="false" 
      android:scrollbars="vertical" /> 

    </RelativeLayout> 
</android.support.v4.widget.DrawerLayout> 
+0

'DrawerLayout'に1つのコンテンツ' View'が必要です。だから、あなたは物事を手配することを意味しますが、引き出しではないものはすべて、あなたの引き出しの上の一つの 'ViewGroup'にあるべきです。'。 –

+0

@MikeMあなたは例を示していただけますか?ありがとうございました – jason

+1

本当に、あなたがしようとしていることを私は伝えることはできません。 'RecyclerView'がメインコンテンツである場合、' container_toolbar'の下にある最も外側の 'LinearLayout'の内側に移動し、' RelativeLayout'と 'FrameLayout'を取り除きます。他に何も入っていない場合、 'container_toolbar'''DinearLayout'も捨てることができます。 –

答えて

1

この階層を作成します。

<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<!-- Main Layout --> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:id="@+id/container_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <include 
      android:id="@+id/toolbar" 
      layout="@layout/toolbar" /> 
    </LinearLayout> 

    <FrameLayout 
     android:id="@+id/container_body" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" /> 
</LinearLayout> 

<!-- Left Drawer --> 
<fragment 
    android:id="@+id/fragment_navigation_drawer" 
    android:layout_width="@dimen/nav_drawer_width" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    app:layout="@layout/fragment_navigation_drawer" 
    android:name="com.findthewayapp.fragments.NavigationDrawerFragment" 
    tools:layout="@layout/fragment_navigation_drawer" /> 
</android.support.v4.widget.DrawerLayout> 
+0

「RecyclerView」はどこにありますか? – jason

+0

@jason私は階層構造の 'RecycleView'を与えているだけです。他のものはすべて' FrameLayout'に入っているか、 'RelativeLayout'を使うことができます。私はあなたに 'Mike M.'のコメントとして' Pattern'を与えています。 – Ironman

+0

だから、 'FrameLayout'の中にRecycleViewを追加すべきですか? – jason

2

すると、このようにアプリケーションバーにツールバーウィジェットを置く -

<android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/appbar" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

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

をそして - アンドロイド:below = "あなたのリサイクラビューの@ + id/appbar"

<android.support.v7.widget.RecyclerView 
     android:id="@+id/my_recycler_view" 
     android:scrollbars="vertical" 
     android:layout_below="@+id/appbar" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

希望すると助かります!

+0

私はrootで 'RelativeLayout'を使うべきですか?ありがとう。 – jason

+0

ええ...問題ありません –

関連する問題