Tôi có một ứng dụng trong Android Studio 1.4 mới (dù sao vấn đề này cũng có mặt tại 1.3.2) và tôi đã quyết định đi với thanh công cụ thay vì thanh tác vụ vì các tính năng mở rộng.Tại sao Android Studio luôn hiển thị ActionBar trong thiết kế ứng dụng, ngay cả khi bị tắt?
Tôi đã đặt cả xml và java tương ứng để ẩn thanh tác vụ và khi được biên dịch, nó không xuất hiện, thay vào đó thanh công cụ ở trên đầu nơi ActionBar sẽ xuất hiện. Nhưng trong quan điểm thiết kế, tôi vẫn thấy nó và không có cách nào để loại bỏ nó. Điều này làm phiền tôi, vì nó che giấu một số khu vực thiết kế và tôi không có đủ không gian cho tất cả các yếu tố của tôi.
Cách xóa ActionBar (1) và thay thế bằng thanh công cụ (2) trong thiết kế Android Studio, sao cho (3) có chiều cao đầy đủ.
Dưới đây là xml và java tác phẩm của tôi, có vẻ như rất nhiều nhưng họ là 90% giá trị mặc định. Tôi thiết lập chủ đề AppTheme trong styles.xml mà có nguồn gốc từ Theme.AppCompat.Light.DarkActionBar và trong manifest tôi nói với trình biên dịch để sử dụng AppTheme.NoActionBar cho chủ đề hoạt động.
activity_unlock.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context="com.example.testapp.UnlockActivity">
<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="@android:color/white" app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_unlock" />
</android.support.design.widget.CoordinatorLayout>
content_unlock.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_unlock" tools:context="com.example.testapp.UnlockActivity">
</RelativeLayout>
UnlockActivity.java
package com.example.testapp;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class UnlockActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_unlock);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
}
Android Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testapp">
<application>
<activity android:name="com.example.testapp.UnlockActivity"
android:label="@string/title_activity_unlock"
android:theme="@style/AppTheme.NoActionBar"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowNoTitle">true</item>
<!--We will be using the toolbar so no need to show ActionBar-->
<item name="android:windowActionBar">false</item>
<!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette-->
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
Chết! Nếu tôi chọn AppTheme.NoActionBar, nó hoạt động như nó trông như thế. Tôi chỉ không nhìn thấy tùy chọn khi tôi lần đầu tiên mở tùy chọn này, nó đã được ẩn theo chủ đề dự án. –
Nhưng rõ ràng đây chỉ là vẻ ngoài của nó. Bạn vẫn cần phải thay đổi chủ đề như được hiển thị trong câu trả lời của bạn cho bản thân ứng dụng để phản ánh phong cách này! – Andy