2014-01-25 56 views
4

我下面的教程的指定覆蓋操作欄佈局上邊距

Overlaying the Action Bar

當我試圖添加

<!-- activity_main.xml in layout folder --> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:paddingTop="@dimen/activity_action_bar_overlay_margin"> 

<!-- dimens.xml in values folder --> 
<dimen name="activity_action_bar_overlay_margin">?attr/actionBarSize</dimen> 

,然後我得到了以下錯誤:

01-25 23:10:33.132: E/AndroidRuntime(554): FATAL EXCEPTION: main 
01-25 23:10:33.132: E/AndroidRuntime(554): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.MainActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown> 
01-25 23:10:33.132: E/AndroidRuntime(554): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 
01-25 23:10:33.132: E/AndroidRuntime(554): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
01-25 23:10:33.132: E/AndroidRuntime(554): at android.app.ActivityThread.access$600(ActivityThread.java:123) 
01-25 23:10:33.132: E/AndroidRuntime(554): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
01-25 23:10:33.132: E/AndroidRuntime(554): at android.os.Handler.dispatchMessage(Handler.java:99) 
01-25 23:10:33.132: E/AndroidRuntime(554): at android.os.Looper.loop(Looper.java:137) 
01-25 23:10:33.132: E/AndroidRuntime(554): at android.app.ActivityThread.main(ActivityThread.java:4424) 
01-25 23:10:33.132: E/AndroidRuntime(554): at java.lang.reflect.Method.invokeNative(Native Method) 
01-25 23:10:33.132: E/AndroidRuntime(554): at java.lang.reflect.Method.invoke(Method.java:511) 
01-25 23:10:33.132: E/AndroidRuntime(554): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
01-25 23:10:33.132: E/AndroidRuntime(554): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
01-25 23:10:33.132: E/AndroidRuntime(554): at dalvik.system.NativeStart.main(Native Method) 
01-25 23:10:33.132: E/AndroidRuntime(554): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown> 
01-25 23:10:33.132: E/AndroidRuntime(554): at android.view.LayoutInflater.createView(LayoutInflater.java:606) 
01-25 23:10:33.132: E/AndroidRuntime(554): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
01-25 23:10:33.132: E/AndroidRuntime(554): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653) 
01-25 23:10:33.132: E/AndroidRuntime(554): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678) 
01-25 23:10:33.132: E/AndroidRuntime(554): at android.view.LayoutInflater.inflate(LayoutInflater.java:466) 
01-25 23:10:33.132: E/AndroidRuntime(554): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
01-25 23:10:33.132: E/AndroidRuntime(554): at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
01-25 23:10:33.132: E/AndroidRuntime(554): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251) 
01-25 23:10:33.132: E/AndroidRuntime(554): at android.app.Activity.setContentView(Activity.java:1835) 
01-25 23:10:33.132: E/AndroidRuntime(554): at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:216) 
01-25 23:10:33.132: E/AndroidRuntime(554): at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:111) 
01-25 23:10:33.132: E/AndroidRuntime(554): at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76) 
01-25 23:10:33.132: E/AndroidRuntime(554): at com.example.myfirstapp.MainActivity.onCreate(MainActivity.java:19) 
01-25 23:10:33.132: E/AndroidRuntime(554): at android.app.Activity.performCreate(Activity.java:4465) 
01-25 23:10:33.132: E/AndroidRuntime(554): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
01-25 23:10:33.132: E/AndroidRuntime(554): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 
01-25 23:10:33.132: E/AndroidRuntime(554): ... 11 more 
01-25 23:10:33.132: E/AndroidRuntime(554): Caused by: java.lang.reflect.InvocationTargetException 
01-25 23:10:33.132: E/AndroidRuntime(554): at java.lang.reflect.Constructor.constructNative(Native Method) 
01-25 23:10:33.132: E/AndroidRuntime(554): at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 
01-25 23:10:33.132: E/AndroidRuntime(554): at android.view.LayoutInflater.createView(LayoutInflater.java:586) 
01-25 23:10:33.132: E/AndroidRuntime(554): ... 26 more 
01-25 23:10:33.132: E/AndroidRuntime(554): Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x2 
01-25 23:10:33.132: E/AndroidRuntime(554): at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:463) 
01-25 23:10:33.132: E/AndroidRuntime(554): at android.view.View.<init>(View.java:2794) 
01-25 23:10:33.132: E/AndroidRuntime(554): at android.view.ViewGroup.<init>(ViewGroup.java:385) 
01-25 23:10:33.132: E/AndroidRuntime(554): at android.widget.LinearLayout.<init>(LinearLayout.java:174) 
01-25 23:10:33.132: E/AndroidRuntime(554): at android.widget.LinearLayout.<init>(LinearLayout.java:170) 
01-25 23:10:33.132: E/AndroidRuntime(554): ... 29 more 

我在想我是否必須使用RelativeLayout才能使用?android:attr/actionBarSize,效果究竟是什麼樣子?以及如何獲得透明的操作欄?

歡呼

+0

你定位了哪些API? – Emmanuel

+0

@Emmanuel'android:minSdkVersion =「7」 android:targetSdkVersion =「19」' – daiyue

回答

7

您的格式是錯誤的,它應該是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:paddingTop="?attr/actionBarSize"> 

你應該dimens.xml定義維度,而不是裏面的佈置。

+1

我修改了原來的文章,因爲我在'dimens.xml'中指定了維度,但是正如你指出的那樣''attr/actionBarSize ''paddingTop'代碼運行正常,所以怎麼了? – daiyue

+1

我剛剛做了文檔說的。我不知道你是否可以在'dimens.xml'中定義'?attrs/actionBarSize' – Emmanuel