【发布时间】:2019-02-20 13:40:53
【问题描述】:
我目前正在尝试在我的应用程序中实现NavigationDrawer,并且一直在遵循 Android 开发人员指南了解如何实现。
但是,当尝试将自定义 Toolbar 设置为 ActionBar 时,Toolbar 不会显示在应用程序中,就好像 setSupprtActionBar() 方法没有执行任何操作一样。
我认为将主题设置为 .NoActionBar 的地方不正确是个问题,但我尝试更改它们但没有成功。
编辑:按照 ʍѳђઽ૯ท 的建议,这里是更新后的代码,仍然没有显示工具栏。
这是我的MainActivity.java:
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Sets custom toolbar as default toolbar and imports menu icon
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Creates navigation drawer
mDrawerLayout = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// set item as selected to persist highlight
menuItem.setChecked(true);
// close drawer when item is tapped
mDrawerLayout.closeDrawers();
// Add code here to update the UI based on the item selected
// For example, swap UI fragments here
return true;
}
});
}
这是我的activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
android:elevation="4dp" />
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="12dp">
<android.support.v7.widget.CardView
android:id="@+id/weather_mode_card"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="14dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:elevation="4dp"
android:padding="10dp">
<ImageView
android:id="@+id/weather_mode_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="6dp"
android:layout_marginEnd="10dp"
android:src="@drawable/icon_weather" />
<TextView
android:id="@+id/weather_mode_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/weather_mode_icon"
android:paddingBottom="5dp"
android:text="@string/weather_mode"
android:textSize="20sp" />
<TextView
android:id="@+id/weather_mode_desc"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_below="@+id/weather_mode_title"
android:layout_toEndOf="@+id/weather_mode_icon"
android:layout_toStartOf="@id/weather_mode_button"
android:text="@string/weather_mode_desc"
android:textColor="#8b8b8b"
android:textSize="14sp" />
<Button
android:id="@+id/weather_mode_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:text="@string/mode_button"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/wifi_mode_card"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="14dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:elevation="4dp"
android:padding="10dp">
<ImageView
android:id="@+id/wifi_mode_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:layout_alignParentTop="true"
android:layout_marginEnd="10dp"
android:src="@drawable/icon_wifi" />
<TextView
android:id="@+id/wifi_mode_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/wifi_mode_icon"
android:paddingBottom="5dp"
android:text="@string/wifi_mode"
android:textSize="20sp" />
<TextView
android:id="@+id/wifi_mode_desc"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_below="@+id/wifi_mode_title"
android:layout_toEndOf="@+id/wifi_mode_icon"
android:layout_toStartOf="@id/wifi_mode_button"
android:text="@string/wifi_mode_desc"
android:textColor="#8b8b8b"
android:textSize="14sp" />
<Button
android:id="@+id/wifi_mode_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:text="@string/mode_button"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/temp_mode_card"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="14dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:elevation="4dp"
android:padding="10dp">
<ImageView
android:id="@+id/temp_mode_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:layout_alignParentTop="true"
android:layout_marginEnd="10dp"
android:src="@drawable/icon_temp" />
<TextView
android:id="@+id/temp_mode_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/temp_mode_icon"
android:paddingBottom="5dp"
android:text="@string/temp_mode"
android:textSize="20sp" />
<TextView
android:id="@+id/temp_mode_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/temp_mode_title"
android:layout_toEndOf="@+id/temp_mode_icon"
android:layout_toStartOf="@id/temp_mode_button"
android:text="@string/temp_mode_desc"
android:textColor="#8b8b8b"
android:textSize="14sp" />
<Button
android:id="@+id/temp_mode_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:text="@string/mode_button"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/drawer_view"
app:headerLayout="@layout/nav_header" />
</android.support.v4.widget.DrawerLayout>
这是我的styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:colorForeground">@color/foreground_material_light</item>
</style>
<style name="AppTheme.NoActionBar" parent="AppTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>
这是我的AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
【问题讨论】:
-
将
AppTheme.NoActionBar的父级设置为AppTheme。 -
你的布局需要重新排列,我建议你按照一些教程什么的。
-
@murtadhaalsabbagh 感谢您的建议。这是我的第一个应用程序,并不确定设计布局的最佳方式。我知道 RecyclerViews 但不确定如何实现它们。如果您对如何改进布局有任何建议,我们将不胜感激。
标签: android android-layout android-studio android-toolbar