2015-10-02 23 views
9

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 đủ.

Android Stdui XML design

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> 

Trả lời

8

Khi bạn có thể tạo bố cục theo bất kỳ cách nào với bất kỳ kiểu nào, bạn có thể xem cách bố cục của mình trông theo các kiểu khác nhau bằng cách nhấp vào nút như được hiển thị trong hình ảnh. Nếu bạn không đặt một kiểu hoặc chủ đề khác trong mã của mình thì chủ đề trong tệp kê khai của bạn sẽ được sử dụng.

enter image description here

+0

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. –

+0

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

7

Đây là khá dễ dàng cuối cùng. Tôi chỉ thay đổi styles.xml từ

<!-- Base application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 

để

<!-- Base application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 

Tôi không có ý tưởng tại sao điều này ảnh hưởng đến quan điểm thiết kế, như khi ra mắt, cả hai phiên bản cung cấp bố trí giống hệt nhau mà không actionbar (như tôi sử dụng AppTheme.NoActionBar được định nghĩa trong styles.xml) Tôi đoán đó là lỗi dựng hình thiết kế Android Studio.

+0

Tôi thực sự không gọi đó là lỗi. Bản xem trước chỉ ở đó để cho bạn biết bố cục của bạn trông như thế nào. Nó chỉ không bận tâm để kiểm tra xem có hay không nó sẽ hiển thị thanh hành động vì đó không phải là mục đích. – BooleanCheese

+1

Nhưng bố cục của tôi chỉ định windowActonBar sai, vì vậy nó sẽ hiển thị mà không có nó –

Các vấn đề liên quan