导航抽屉问题(未显示布局预览)

时间:2018-05-06 15:21:25

标签: android android-layout android-studio navigation-drawer android-navigation-drawer

我创建了应用程序并且我想使用导航抽屉菜单,但是当我尝试在导航抽屉xml中编辑时,那么问题"等待构建完成..."发生了,我在android studio的左侧看不到布局预览

关联

activity_main_drawer

此处未找到layout_preview

layout_preview

我试过soultion来解决这个问题,但遗憾的是没有为我工作

这是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;
    }
}
  

更新1 :经过多次尝试后,在将IDE更新到版本3.1.2之后,它似乎是一个普遍的问题,到目前为止只有一个solution @ mtak建议虽然它类似于预览中的顶级菜单选项

6 个答案:

答案 0 :(得分:15)

删除行

tools:showIn="navigation_view"

来自activity_main_drawer.xml并重建。 这为我解决了同样的问题。 不知道为什么!!!

问题已在AS 3.1.3(2018年6月8日)中解决,再次出现(2018年6月16日)!!!

新的临时解决方法:

  1. 从菜单文件中剪切tools:showIn="navigation_view"行。
  2. 关闭菜单文件。
  3. 重新打开并粘贴该行。
  4. 转到设计并按原样查看菜单。
  5. 如果您关闭菜单文件并重新打开它,问题就会恢复! 仍无法预览文字。

答案 1 :(得分:4)

You can try the following:

  • Run Build then try to see the preview again

  • Close the current layout,open another then reopen again

答案 2 :(得分:0)

我找到了解决方案。删除这两个工具行并关闭/重新打开:

  • xmlns:tools =“ http://schemas.android.com/tools”

  • tools:showIn =“ navigation_view”

答案 3 :(得分:0)

由于扩展了AppCompatActivity,因此需要确保在 styles.xml 中,您的AppTheme是AppCompat的后代

    async presentAlertMultipleButtons() {
        const alert = await this.alertController.create({
            header: 'Confirm',
            subHeader: 'Your dress is added',
            message: 'Admin team will review the dress and post it as soon as possible.',
            buttons: ['Ok'],
            mode: 'ios'
        });

        await alert.present();
        const post_data = {
            'app_id': 'xxxxxxxxxxxxxxxxxxxxxx',
            'contents': {
                'en': 'new dress added to fostania'
            },
            'headings': {
                'en': 'new dress'
            },
            'included_segments': ['admins'],
        }
        const httpOptions2 = {
            headers: new HttpHeaders({
                'Content-Type': 'application/json',
                'Authorization': 'Basic xxxxxxxxxxxxxxxxxxxxxxxxx'
            })
        };
        this.httpClient.post('https://onesignal.com/api/v1/notifications', post_data, httpOptions2);

        this.router.navigate(['/list']);
    }

这对我很有效,而对于没有帮助的答案却是可接受的。

答案 4 :(得分:0)

实际上这个问题是因为您使用 AppComactActivity 扩展了您的 Activity,并且您的主题父主题不是 Theme.AppCompat 在应用程序标签下的 manifest.xml 中将此主题用作您的默认主题。你的风格应该是这样的

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
 </style>


答案 5 :(得分:-2)

重建对我有用的项目