Problem copying AbsoluteLayout - com.android.internal.R cannot be resolved

别说谁变了你拦得住时间么 提交于 2020-04-20 16:57:51

问题


I came to a problem that the only solutions is using a AbsoluteLayout (Just to show something at specific positions).

I'm trying to copy the AbsoluteLayout class to avoid it being removed on future releases and my app stop working.

I'm copying from here: http://www.google.com/codesearch#cZwlSNS7aEw/frameworks/base/core/java/android/widget/AbsoluteLayout.java&exact_package=android&q=AbsoluteLayout&type=cs

But getting this code, first, I changed all the mPadding(Direction) to getPadding(Direction), but there's still an error on the LayoutParams constructor:

    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

How can I get these values? Or someone already has this class independently that don't belong to google wishes to keep on the API?


回答1:


You need to copy over declare-styleable entry from attrs.xml:

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

Just add res/values/attrs.xml file to your application and copy above lines there.


When this is done, update your code to reference R from your package:

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();
}


来源:https://stackoverflow.com/questions/7068529/problem-copying-absolutelayout-com-android-internal-r-cannot-be-resolved

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!