Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 具有自定义xml属性的自定义视图:如何引用布局_Android_Xml_Android Custom View - Fatal编程技术网

Android 具有自定义xml属性的自定义视图:如何引用布局

Android 具有自定义xml属性的自定义视图:如何引用布局,android,xml,android-custom-view,Android,Xml,Android Custom View,有人知道如何在我的自定义小部件的代码中以编程方式获取引用的xml布局吗。我已经用我想要的属性创建了一个自定义的declare styleable,并且我知道如何获取更多的xml属性值,比如字符串或整数 我想做的是这样的: <MyCustomView xmlns:my="http://schemas.android.com/apk/res-auto" id="@+id/view" my:headerLayout="@layout/my_fancy_layout"

有人知道如何在我的自定义小部件的代码中以编程方式获取引用的xml布局吗。我已经用我想要的属性创建了一个自定义的declare styleable,并且我知道如何获取更多的xml属性值,比如字符串或整数

我想做的是这样的:

<MyCustomView
    xmlns:my="http://schemas.android.com/apk/res-auto"
    id="@+id/view"
    my:headerLayout="@layout/my_fancy_layout"
    />
TypedArray attrs = ... ;
int headerRes = attrs.getResourceId(R.styleable.MyCustomWidget_headerLayout, -1);
因此,我想以编程方式检索我的布局,并在MyCustomView的代码中扩展该布局

你知道怎么做吗

编辑:我想我可以用

int resId=attrs.getAttributeResourceValueandroidns,headerLayout,0

但是,如果我的MyCustomView是一个库项目,并且如果我想使用

我的=http://schemas.android.com/apk/res-auto


您确实可以在自定义视图的构造函数中放大布局:

public class MyCustomView extends /* LinearLayout, RelativeLayout, etc. */ {
    public MyCustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }
    public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context, attrs);
    }
    protected void initView(Context context, attrs) {
        LayoutInflater.from(context).inflate(attrs.getAttributeResourceValue("http://schemas.android.com/apk/res-auto", "headerLayout", 0), this, true);
    }
}

好的,我自己找到了解决办法:

您必须从属性集中检索TypedArray

然后,您可以通过以下方式访问所需的资源id:

<MyCustomView
    xmlns:my="http://schemas.android.com/apk/res-auto"
    id="@+id/view"
    my:headerLayout="@layout/my_fancy_layout"
    />
TypedArray attrs = ... ;
int headerRes = attrs.getResourceId(R.styleable.MyCustomWidget_headerLayout, -1);
您可以像平常一样充气:

LayoutInflater.from(context).inflate(headerRes, this);

是的,这很清楚,但我想用xml指定布局。因此,我想用xml中指定的值替换R.layout.my_custom_view的值