2011-08-15 20 views
0

我遇到了一個問題,即唯一的解決方案是使用AbsoluteLayout(只是在特定位置顯示某些東西)。問題複製AbsoluteLayout - com.android.internal.R無法解析

我試圖複製AbsoluteLayout類,以避免在將來的版本中刪除它,並且我的應用程序停止工作。

我從這裏複製:http://www.google.com/codesearch#cZwlSNS7aEw/frameworks/base/core/java/android/widget/AbsoluteLayout.java&exact_package=android&q=AbsoluteLayout&type=cs

但是,讓這個問題,首先,我改變了所有的mPadding(方向)getPadding(方向),但仍然是一個錯誤的的LayoutParams構造:

public LayoutParams(Context c, AttributeSet attrs) { 
     super(c, attrs); 
     TypedArray a = c.obtainStyledAttributes(attrs, 
       com.android.internal.R.styleable.AbsoluteLayout_Layout); 
     x = a.getDimensionPixelOffset(
       com.android.internal.R.styleable.AbsoluteLayout_Layout_layout_x, 0); 
     y = a.getDimensionPixelOffset(
       com.android.internal.R.styleable.AbsoluteLayout_Layout_layout_y, 0); 
     a.recycle(); 
    } 

com.android.internal.R cannot be resolved to a variable 

我怎樣才能得到這些值?或者某人已經獨立擁有這個不屬於Google的類希望繼續使用API​​?

回答

8

您需要通過declare-styleable進入從attrs.xml複製:

<declare-styleable name="AbsoluteLayout_Layout"> 
    <attr name="layout_x" format="dimension" /> 
    <attr name="layout_y" format="dimension" /> 
</declare-styleable> 

只需添加res/values/attrs.xml文件到您的應用程序並複製線那裏。


這樣做時,更新您的代碼從你的包中引用R

import com.your.package.R; 
... 
public LayoutParams(Context c, AttributeSet attrs) { 
    super(c, attrs); 
    TypedArray a = c.obtainStyledAttributes(attrs, 
      R.styleable.AbsoluteLayout_Layout); 
    x = a.getDimensionPixelOffset(
      R.styleable.AbsoluteLayout_Layout_layout_x, 0); 
    y = a.getDimensionPixelOffset(
      R.styleable.AbsoluteLayout_Layout_layout_y, 0); 
    a.recycle(); 
} 
+0

泰!這工作完美。 –