AppCompat v7 和 DatePicker 呈现问题
AppCompat v7 and DatePicker render issue
最近我读了 The Big Nerd Ranch Guide(第 2 版) 中关于对话的一章,我已经按照书上的内容做了所有的事情。但也有一些问题,书中没有提到。 Intellij Idea 的预览区域无法与 AppCompat 主题一起正常工作。
这是我当前的主题:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
这是我在预览区看到的:
我在网上了解到使用 Base.Theme.AppCompat.Light.DarkActionBar 可能会有帮助。它删除了消息,但 ActionBar 也消失了。
哦,DatePicker 不适用于任何 Material Design:
这只是一个渲染问题,因为这两个主题在设备上都能正常工作。 有人可以尝试在 IDEA 和 Android Studio 中使用 AppCompat 主题预览 DatePicker 吗? 可能这只是一个 IDEA 问题。
P.S. dialog_date.xml
<?xml version="1.0" encoding="utf-8"?>
<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_date_date_picker"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:calendarViewShown="false"
/>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.criminalintent" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".CrimeListActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".CrimePagerActivity"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
使您的 style.xml 看起来像这样,工具栏将显示:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
你的 DatePicker 需要像这样:
<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="spinner"
android:id="@+id/datePicker" />
如果您在 xml 中需要 CalendarView 添加 DatePicker:
<CalendarView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/calendarView1" />
切换主题
如果您查看 Theme.AppCompat.Light.DarkActionBar
的声明,它直接在 AppCompat 的 values/themes.xml 文件中别名为 Base.Theme.AppCompat.Light.DarkActionBar
:
<!-- Platform-independent theme providing an action bar in a dark-themed activity. -->
<style name="Theme.AppCompat.Light.DarkActionBar" parent="Base.Theme.AppCompat.Light.DarkActionBar" />
这在任何其他设备配置中都没有别名(例如 values-v23/themes.xml、values-v18/themes.xml 等)。查看 AppCompat here 的资源。这意味着对于每个设备,Theme.AppCompat.Light.DarkActionBar
将 始终且仅 别名到 Base.Theme.AppCompat.Light.DarkActionBar
。
Base
主题的文档使这一点更加清晰:
Themes in the "Base.Theme" family vary based on the current
platform version to provide the correct basis on each device. You
probably don't want to use them directly in your apps.
Themes in the "Theme.AppCompat" family are meant to be extended or
used directly by apps.
因此,您从 Theme.AppCompat.Light.DarkActionBar
切换到 Base.Theme.Light.DarkActionBar
绝不会影响实际设备上的显示方式,这实际上不是更改。 Android Studio 行为不同的事实是 Android Studio 的一个问题(我无法在 AS 2.0 预览版 9 上使用您的代码重现)。
Android Studio
有问题
我会说“设计”选项卡一直有点不稳定。它越来越好,但并不总是很好,特别是在支持库的东西上。更改您的应用程序的主题(即使我上面所说的不是真的)只是为了使“设计”选项卡看起来漂亮可能不是一个好主意。想想你的用户。如果您真的想知道您的屏幕是什么样子,那么将它部署到真实设备或模拟器上。
对话框
我也无法在 AS 1.4(稳定版)或 AS 2.0 预览版 9(Canary、ATM)中重现您的对话问题。但是由于您的整个布局文件是 DatePicker
,我认为您不会从中受益。当您考虑到您的 DatePicker
将显示在对话框中(“设计”选项卡无法模拟)时,这就更没有用了。我并不是要听起来刺耳,但您可能只需要硬着头皮使用模拟器或物理设备。
糟糕 Android Studio 1.5.0 应该更新到 1.5.1。如此简单
最近我读了 The Big Nerd Ranch Guide(第 2 版) 中关于对话的一章,我已经按照书上的内容做了所有的事情。但也有一些问题,书中没有提到。 Intellij Idea 的预览区域无法与 AppCompat 主题一起正常工作。
这是我当前的主题:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
这是我在预览区看到的:
我在网上了解到使用 Base.Theme.AppCompat.Light.DarkActionBar 可能会有帮助。它删除了消息,但 ActionBar 也消失了。
哦,DatePicker 不适用于任何 Material Design:
这只是一个渲染问题,因为这两个主题在设备上都能正常工作。 有人可以尝试在 IDEA 和 Android Studio 中使用 AppCompat 主题预览 DatePicker 吗? 可能这只是一个 IDEA 问题。
P.S. dialog_date.xml
<?xml version="1.0" encoding="utf-8"?>
<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_date_date_picker"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:calendarViewShown="false"
/>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.criminalintent" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".CrimeListActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".CrimePagerActivity"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
使您的 style.xml 看起来像这样,工具栏将显示:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
你的 DatePicker 需要像这样:
<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="spinner"
android:id="@+id/datePicker" />
如果您在 xml 中需要 CalendarView 添加 DatePicker:
<CalendarView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/calendarView1" />
切换主题
如果您查看 Theme.AppCompat.Light.DarkActionBar
的声明,它直接在 AppCompat 的 values/themes.xml 文件中别名为 Base.Theme.AppCompat.Light.DarkActionBar
:
<!-- Platform-independent theme providing an action bar in a dark-themed activity. -->
<style name="Theme.AppCompat.Light.DarkActionBar" parent="Base.Theme.AppCompat.Light.DarkActionBar" />
这在任何其他设备配置中都没有别名(例如 values-v23/themes.xml、values-v18/themes.xml 等)。查看 AppCompat here 的资源。这意味着对于每个设备,Theme.AppCompat.Light.DarkActionBar
将 始终且仅 别名到 Base.Theme.AppCompat.Light.DarkActionBar
。
Base
主题的文档使这一点更加清晰:
Themes in the "Base.Theme" family vary based on the current platform version to provide the correct basis on each device. You probably don't want to use them directly in your apps.
Themes in the "Theme.AppCompat" family are meant to be extended or used directly by apps.
因此,您从 Theme.AppCompat.Light.DarkActionBar
切换到 Base.Theme.Light.DarkActionBar
绝不会影响实际设备上的显示方式,这实际上不是更改。 Android Studio 行为不同的事实是 Android Studio 的一个问题(我无法在 AS 2.0 预览版 9 上使用您的代码重现)。
Android Studio
有问题我会说“设计”选项卡一直有点不稳定。它越来越好,但并不总是很好,特别是在支持库的东西上。更改您的应用程序的主题(即使我上面所说的不是真的)只是为了使“设计”选项卡看起来漂亮可能不是一个好主意。想想你的用户。如果您真的想知道您的屏幕是什么样子,那么将它部署到真实设备或模拟器上。
对话框
我也无法在 AS 1.4(稳定版)或 AS 2.0 预览版 9(Canary、ATM)中重现您的对话问题。但是由于您的整个布局文件是 DatePicker
,我认为您不会从中受益。当您考虑到您的 DatePicker
将显示在对话框中(“设计”选项卡无法模拟)时,这就更没有用了。我并不是要听起来刺耳,但您可能只需要硬着头皮使用模拟器或物理设备。
糟糕 Android Studio 1.5.0 应该更新到 1.5.1。如此简单