android - 如何使用兼容性库获取 "?android:attr/actionBarSize"

标签 android android-layout android-compatibility

我正在尝试在 Android 2.2 项目中使用 Fragments 和 ActionBar。使用“?android:attr/actionBarSize”时出现错误,如何正确获取和设置该值?

例子:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/frags">

    <fragment class="com.example.android.hcgallery.TitlesFragment"
            android:id="@+id/frag_title"
            android:visibility="gone"
            android:layout_marginTop="?android:attr/actionBarSize"
            android:layout_width="@dimen/titles_size"
            android:layout_height="match_parent" />

    <fragment class="com.example.android.hcgallery.ContentFragment"
            android:id="@+id/frag_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

</LinearLayout>

错误:找不到与给定名称匹配的资源(在值为“?android:attr/actionBarSize”的“layout_marginTop”处)。

最佳答案

你应该只使用:

android:layout_marginTop="?attr/actionBarSize"

参见 http://developer.android.com/training/basics/actionbar/overlaying.html

"...The one with the android: prefix is for versions of Android that include the style in the platform and the one without the prefix is for older versions that read the style from the Support Library..."

为我替换

android:layout_marginTop="?android:attr/actionBarSize" 

android:layout_marginTop="?attr/actionBarSize" 

帮助在 Android 2.2 和 Android 4.3 上顺利启动应用程序。

关于android - 如何使用兼容性库获取 "?android:attr/actionBarSize",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7760817/

相关文章:

android - 使用android SDK将文件直接上传到存储桶中的文件夹

java - 如何将文本值从 ViewHolder 发送到 ItemTouchHelper 类?

来自 XML 的 Android 文本输入

android - 在 Android 中组合布局

android - 在 Android 上将某些版本的自适应图标显示为 ImageView

android - 我的应用程序与 Google Play 商店上的 Nexus 7 不兼容

android - VectorDrawableCompat.create 无法在旧设备上从 vector.xml 创建图像

android - "Now on Tap"安卓模拟器功能测试

java - Android - 如何从通话记录中获取联系人?

android - NavigationDrawer isDrawerOpened 总是返回 true