mic*_*vka 10 java xml android android-layout android-studio
我在新的Android Studio 1.4中有一个应用程序(尽管如此,这个问题也出现在1.3.2)并且由于扩展功能,我决定使用工具栏而不是操作栏.
我已经相应地设置了xml和java来隐藏动作栏,并且在编译时它不存在,而是工具栏位于ActionBar出现的顶部.但在设计视图中我仍然看到它并且没有办法将其删除.这困扰我,因为它隐藏了一些设计区域,我没有足够的空间容纳我的所有元素.
如何删除ActionBar(1)并将其替换为Android Studio设计中的toolbar(2),以便(3)具有完整高度.
这是我的xml和java文件,看起来很多,但它们是90%的默认值.我在styles.xml中设置AppTheme主题,该主题派生自Theme.AppCompat.Light.DarkActionBar,在清单中我告诉编译器使用AppTheme.NoActionBar作为活动主题.
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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
这最终很容易.我刚改变styles.xml了
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
Run Code Online (Sandbox Code Playgroud)
至
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
Run Code Online (Sandbox Code Playgroud)
我不知道为什么这会影响设计器视图,因为在启动时,两个版本都提供了相同的布局而没有操作栏(我使用的AppTheme.NoActionBar是定义的styles.xml)我想这是一个Android Studio设计渲染错误.
| 归档时间: |
|
| 查看次数: |
18396 次 |
| 最近记录: |