Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

android - navigation drawer issue (not showing layout preview)

I created the app and I want to use navigation drawer menu, but when I tried to edit in navigation drawer xml, then the problem "Waiting for build to finish..." happened and I don't see layout previw on left side of android studio

to relate

activity_main_drawer

layout_preview not found here

layout_preview

I tried this soultion to solve this issue but unfortunately not working for me

this is activity_main_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/articles"
            android:icon="@drawable/ic_menu_camera"
            android:title="@string/articles" />
        <item
            android:id="@+id/windows"
            android:icon="@drawable/ic_menu_gallery"
            android:title="@string/windows" />
        <item
            android:id="@+id/linux"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="@string/linux" />
        <item
            android:id="@+id/miscellaneous_devices"
            android:icon="@drawable/ic_menu_manage"
            android:title="@string/miscellaneous_devices" />

        <item
            android:id="@+id/information_security"
            android:icon="@drawable/ic_menu_manage"
            android:title="@string/information_security" />

        <item
            android:id="@+id/facebook"
            android:icon="@drawable/ic_menu_manage"
            android:title="@string/facebook" />
    </group>

    <item android:title="Communicate">
        <menu>
            <item
                android:id="@+id/nav_share"
                android:icon="@drawable/ic_menu_share"
                android:title="Share" />
            <item
                android:id="@+id/nav_send"
                android:icon="@drawable/ic_menu_send"
                android:title="Send" />
        </menu>
    </item>

</menu>

activity_main

    package www.pro.cs_is.com.procsis;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

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



        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.miscellaneous_devices) {
            // Handle the camera action
        } else if (id == R.id.articles) {

        } else if (id == R.id.windows) {

        } else if (id == R.id.linux) {

        } else if (id == R.id.facebook) {

        } else if (id == R.id.information_security) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

Update 1 : After many attempts it's seems a general Issue after update IDE to version 3.1.2, till now there's only one solution which @mtak suggested although it is similar to the top menu options in the preview

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Remove the line

tools:showIn="navigation_view"

from activity_main_drawer.xml and rebuild. This solved the same problem for me. Don't know why!!!

Problem solved in AS 3.1.3(8 June 2018) and reappeared again (16 June 2018)!!!

New temporary workaround:

  1. Cut the line tools:showIn="navigation_view" from the menu file.
  2. Close the menu file.
  3. Reopen it and paste the line.
  4. Go to design and see the menu as it should be.

If you close the menu file and reopen it the problem comes back! Still no preview in Text.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...