Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.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按属性查找自定义视图_Android_Android Custom View - Fatal编程技术网

android按属性查找自定义视图

android按属性查找自定义视图,android,android-custom-view,Android,Android Custom View,我正在使用一个布局,它承载许多自定义视图。每个自定义视图都有“myname:num”属性,该属性保存唯一的数值 问题:如何通过“num”属性找到视图?通常我会使用findviewbyd()方法,但由于“android:id”不能是数字,我必须找到另一个解决方案 布局定义: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/a

我正在使用一个布局,它承载许多自定义视图。每个自定义视图都有“myname:num”属性,该属性保存唯一的数值

问题:如何通过“num”属性找到视图?通常我会使用findviewbyd()方法,但由于“android:id”不能是数字,我必须找到另一个解决方案

布局定义:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myname="http://schemas.android.com/apk/res/com.example"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <com.example.custom.MyView
            myname:num="17"
            myname:type="2" />
        <com.example.custom.MyView
            myname:num="18"
            myname:type="2" />
        <com.example.custom.MyView
            myname:num="19"
            myname:type="2" />
    </LinearLayout>
</LinearLayout>

给每个视图一个id,然后在活动中,您可以使用

int dynamicId = getResources().getIdentifier("*unique_id*", "id", "*package_name*");
findViewById(dynamicId);
因此,如果您以增量方式将每个视图标识为
view\u 1
view\u 2
<代码>查看\u numview然后可以使用类似循环的方式动态获取它们

for(int i = 1; i <= numViews; i ++){
    int dynamicId = getResources().getIdentifier("view_" + i, "id", "*package_name*");
    MyView view = (MyView) findViewById(dynamicId);
}

for(inti=1;我和你应该缓存所有这些,否则速度会非常慢。