我创建了应用程序,我想使用导航抽屉菜单,但当我尝试在导航抽屉xml中编辑时,问题“等待构建完成...”发生了,我没有看到左侧的布局预设安卓工作室
关联
此处未找到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建议,尽管它类似于预览中的顶级菜单选项
删除该行
tools:showIn="navigation_view"
来自activity_main_drawer.xml并重建。这为我解决了同样的问题。不知道为什么!!!
AS 3.1.3(2018年6月8日)解决了问题并再次出现(2018年6月16日)!
新的临时解决方法:
tools:showIn="navigation_view"
行。如果您关闭菜单文件并重新打开它,问题就会恢复!文本中仍然没有预览。
您可以尝试以下方法:
因为您扩展了AppCompatActivity,所以您需要确保在styles.xml中AppEme是AppCompat的后代。
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
这对我有用,而接受的答案却没有帮助。
我找到了解决方案。删除这两个工具行并关闭/重新打开:
重建项目对我有用