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

fla*_*ack 8 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>
Run Code Online (Sandbox Code Playgroud)

错误:找不到与给定名称匹配的资源(在'layout_marginTop'中,值为'?android:attr/actionBarSize').

小智 15

你应该使用:

android:layout_marginTop="?attr/actionBarSize"
Run Code Online (Sandbox Code Playgroud)

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

"...带有android:前缀的版本适用于包含平台样式的Android版本,没有前缀的版本适用于从支持库中读取样式的旧版本..."

为我替换

android:layout_marginTop="?android:attr/actionBarSize" 
Run Code Online (Sandbox Code Playgroud)

android:layout_marginTop="?attr/actionBarSize" 
Run Code Online (Sandbox Code Playgroud)

帮助在Android 2.2和Android 4.3上启动应用程序没有任何问题.


小智 2

我认为问题是actionbar是android 3.0自带的,而你使用的是2.2。但有一种方法可以通过名为“actionbarsherlock”的扩展来跳过此操作。

  • 那么,您采取了什么措施来解决这个问题?如果您找到答案,请务必回答您的问题。对于未来的用户来说可能会派上用场。 (8认同)