Android 活动设计预览中缺少元素
我的应用程序在运行时与在“设计”选项卡中编辑时不同。为什么会这样?一个例子是ActionBar中没有显示的选项。活动内容如下: activity_home.xmlAndroid 活动设计预览中缺少元素,android,android-studio,Android,Android Studio,我的应用程序在运行时与在“设计”选项卡中编辑时不同。为什么会这样?一个例子是ActionBar中没有显示的选项。活动内容如下: activity_home.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" androi
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fafafa">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@drawable/gradient"
android:elevation="4dp"
android:textAlignment="center"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />
<Button
android:id="@+id/button2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="text"
android:textSize="22dp"
android:padding="10dp"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:background="@color/colorPrimaryDark"
android:layout_centerInParent="true"
/>
</RelativeLayout>
在这里,操作栏中的选项菜单不显示(其他一切显示正常)。当我运行应用程序时,选项菜单会很好地显示出来。知道为什么吗?您是通过重写OnCreateOptions菜单而不是通过XML设计来创建选项菜单,这就是为什么它不会显示在预览中。如果要删除选项菜单,只需删除该重写方法。您的R.menu.home\u菜单呢?您在其中定义了项目吗?在android:showAsAction=“always”或app:showAsAction=“always”中使用此项目?
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/settings"
android:title="@string/settings" />
<item
android:id="@+id/dark_theme"
android:title="@string/dark_theme" />
</menu>
public class Home extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home_menu, menu);
return true;
}
}