2016-06-24 8 views
1

Erstens hatte ich eine Symbolleiste, dann habe ich eine Recycler-Ansicht hinzugefügt, aber jetzt ist die Symbolleiste weg. Ich habe alles versucht, um die Symbolleiste anzuzeigen, aber ich habe immer versagt. Kannst du mir sagen, wie ich die Toolbar zurückbekomme? Danke im Voraus. Hier ist mein Java-Code:Symbolleiste ist nicht sichtbar auf 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); 


    } 

Hier ist der Code Layout:

<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

Sie möchten nur einen Inhalt 'View' in einem' DrawerLayout'. Also, wie auch immer du es vorhast, Dinge zu arrangieren, alles, was nicht die Schublade ist, sollte sich alle in einer "ViewGroup" über deiner Schublade "" befinden. –

+0

@MikeM können Sie bitte mit einem Beispiel zeigen? Vielen Dank – jason

+1

Nicht wirklich, weil ich nicht sagen kann, was du versuchst zu tun. Wenn der 'RecyclerView' der Hauptinhalt sein soll, dann verschiebe ihn einfach in das äußerste' LinearLayout', unter 'container_toolbar', und entferne die' RelativeLayout' und 'FrameLayout'. Sie können auch die 'container_toolbar'' LinearLayout' weglassen, wenn nichts anderes drin ist. –

Antwort

1

Machen Sie diese Hierarchie.

<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

Wo ist 'RecyclerView' hin? – jason

+0

@jason Ich gebe nur die Hierarchie "RecycleView" und alle anderen Sachen kommen in 'FrameLayout' oder Sie können' RelativeLayout' verwenden. Ich gebe dir nur "Pattern" als "Mike M" kommentieren. – Ironman

+0

Also, sollte ich RecycleView in 'FrameLayout' hinzufügen? – jason

2

Ihre Symbolleiste Widget wie dies in AppBar Put -

<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> 

Und dann - android : unter = "@ + ID/appbar in Ihrem Recyclerview"

<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"/> 

Ich hoffe, es hilft!

+0

Soll ich ein 'RelativeLayout' im root verwenden? Vielen Dank. – jason

+0

Ja ... Kein Problem –