向某个viewgroup中插入resource

本文介绍如何使用LayoutInflater加载资源文件并将其填充到指定的父视图中。通过LayoutInflater的inflate方法可以轻松实现界面布局的动态加载。

    LayoutInflater li = this.getLayoutInflater();
    li.inflate(resource, root)

页面信息: 这是某一个按钮未展开情况下的信息: <?xml version='1.0' encoding='UTF-8' standalone='yes' ?> <hierarchy index="0" class="hierarchy" rotation="0" width="1080" height="2424"> <android.widget.FrameLayout index="0" package="com.brother.ptouch.iprintandlabel" class="android.widget.FrameLayout" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,142][1080,1075]" displayed="true" a11y-important="true" screen-reader-focusable="false" drawing-order="0" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false"> <android.widget.LinearLayout index="0" package="com.brother.ptouch.iprintandlabel" class="android.widget.LinearLayout" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,142][1080,1075]" displayed="true" a11y-important="false" screen-reader-focusable="false" drawing-order="1" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false"> <android.widget.FrameLayout index="0" package="com.brother.ptouch.iprintandlabel" class="android.widget.FrameLayout" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,142][1080,1075]" displayed="true" a11y-important="false" screen-reader-focusable="false" drawing-order="2" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false"> <android.widget.FrameLayout index="0" package="com.brother.ptouch.iprintandlabel" class="android.widget.FrameLayout" text="" resource-id="com.brother.ptouch.iprintandlabel:id/action_bar_root" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,142][1080,1075]" displayed="true" a11y-important="false" screen-reader-focusable="false" drawing-order="1" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false"> <android.widget.FrameLayout index="0" package="com.brother.ptouch.iprintandlabel" class="android.widget.FrameLayout" text="" resource-id="android:id/content" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,142][1080,1075]" displayed="true" a11y-important="false" screen-reader-focusable="false" drawing-order="1" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false"> <androidx.appcompat.widget.LinearLayoutCompat index="0" package="com.brother.ptouch.iprintandlabel" class="androidx.appcompat.widget.LinearLayoutCompat" text="" resource-id="com.brother.ptouch.iprintandlabel:id/parentPanel" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,142][1080,1075]" displayed="true" a11y-important="false" screen-reader-focusable="false" drawing-order="1" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false"> <android.widget.FrameLayout index="0" package="com.brother.ptouch.iprintandlabel" class="android.widget.FrameLayout" text="" resource-id="com.brother.ptouch.iprintandlabel:id/customPanel" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,142][1080,1075]" displayed="true" a11y-important="false" screen-reader-focusable="false" drawing-order="3" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false"> <android.widget.FrameLayout index="0" package="com.brother.ptouch.iprintandlabel" class="android.widget.FrameLayout" text="" resource-id="com.brother.ptouch.iprintandlabel:id/custom" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,142][1080,1075]" displayed="true" a11y-important="false" screen-reader-focusable="false" drawing-order="1" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false"> <android.widget.LinearLayout index="0" package="com.brother.ptouch.iprintandlabel" class="android.widget.LinearLayout" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="true" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,142][1080,1075]" displayed="true" a11y-important="true" screen-reader-focusable="false" drawing-order="1" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false"> <android.view.ViewGroup index="0" package="com.brother.ptouch.iprintandlabel" class="android.view.ViewGroup" text="" resource-id="com.brother.ptouch.iprintandlabel:id/print_option_toolbar" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,142][1080,289]" displayed="true" a11y-important="false" screen-reader-focusable="false" drawing-order="1" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false"> <android.widget.ImageButton index="0" package="com.brother.ptouch.iprintandlabel" class="android.widget.ImageButton" text="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,142][147,289]" displayed="true" a11y-important="true" screen-reader-focusable="false" drawing-order="2" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false" /> <android.widget.TextView index="1" package="com.brother.ptouch.iprintandlabel" class="android.widget.TextView" text="Print Options" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[189,180][511,251]" displayed="true" a11y-important="true" screen-reader-focusable="false" drawing-order="1" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false" /> <android.widget.Button index="2" package="com.brother.ptouch.iprintandlabel" class="android.widget.Button" text="PRINT" resource-id="com.brother.ptouch.iprintandlabel:id/printer_option_printer" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[849,152][1080,278]" displayed="true" a11y-important="true" screen-reader-focusable="false" drawing-order="3" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false" /> </android.view.ViewGroup> <android.widget.ScrollView index="1" package="com.brother.ptouch.iprintandlabel" class="android.widget.ScrollView" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="true" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,289][1080,1075]" displayed="true" a11y-important="true" screen-reader-focusable="false" drawing-order="2" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false"> <android.widget.LinearLayout index="0" package="com.brother.ptouch.iprintandlabel" class="android.widget.LinearLayout" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,289][1080,1075]" displayed="true" a11y-important="false" screen-reader-focusable="false" drawing-order="1" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false"> <android.widget.RelativeLayout index="0" package="com.brother.ptouch.iprintandlabel" class="android.widget.RelativeLayout" text="" resource-id="com.brother.ptouch.iprintandlabel:id/option_print_lay" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,289][1080,484]" displayed="true" a11y-important="true" screen-reader-focusable="false" drawing-order="1" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false"> <android.widget.TextView index="0" package="com.brother.ptouch.iprintandlabel" class="android.widget.TextView" text="Printer" resource-id="com.brother.ptouch.iprintandlabel:id/option_printer_title" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[42,331][152,382]" displayed="true" a11y-important="true" screen-reader-focusable="false" drawing-order="1" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false" /> <android.widget.TextView index="1" package="com.brother.ptouch.iprintandlabel" class="android.widget.TextView" text="PT-P950NW(Not connected)" resource-id="com.brother.ptouch.iprintandlabel:id/option_printer_name" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="true" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[42,382][1038,439]" displayed="true" a11y-important="true" screen-reader-focusable="false" drawing-order="2" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false" /> <android.view.View index="2" package="com.brother.ptouch.iprintandlabel" class="android.view.View" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[42,460][1038,463]" displayed="true" a11y-important="false" screen-reader-focusable="false" drawing-order="3" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false" /> </android.widget.RelativeLayout> <android.widget.RelativeLayout index="1" package="com.brother.ptouch.iprintandlabel" class="android.widget.RelativeLayout" text="" resource-id="com.brother.ptouch.iprintandlabel:id/option_resolution_lay" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,484][1080,685]" displayed="true" a11y-important="false" screen-reader-focusable="false" drawing-order="2" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false"> <android.widget.TextView index="0" package="com.brother.ptouch.iprintandlabel" class="android.widget.TextView" text="Resolution" resource-id="com.brother.ptouch.iprintandlabel:id/option_resolution_title" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[42,526][216,577]" displayed="true" a11y-important="true" screen-reader-focusable="false" drawing-order="1" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false" /> <android.widget.Spinner index="1" package="com.brother.ptouch.iprintandlabel" class="android.widget.Spinner" text="" resource-id="com.brother.ptouch.iprintandlabel:id/option_resolution_options" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="true" password="false" scrollable="true" selected="false" bounds="[42,577][1038,640]" displayed="true" a11y-important="true" screen-reader-focusable="false" drawing-order="2" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false"> <android.widget.TextView index="0" package="com.brother.ptouch.iprintandlabel" class="android.widget.TextView" text="Normal" resource-id="com.brother.ptouch.iprintandlabel:id/resolution_spinner_txtV" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="true" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[42,580][912,637]" displayed="true" a11y-important="true" screen-reader-focusable="false" drawing-order="1" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false" /> </android.widget.Spinner> <android.view.View index="2" package="com.brother.ptouch.iprintandlabel" class="android.view.View" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[42,661][1038,664]" displayed="true" a11y-important="false" screen-reader-focusable="false" drawing-order="3" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false" /> </android.widget.RelativeLayout> <android.widget.RelativeLayout index="2" package="com.brother.ptouch.iprintandlabel" class="android.widget.RelativeLayout" text="" resource-id="com.brother.ptouch.iprintandlabel:id/option_copies_lay" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,685][1080,880]" displayed="true" a11y-important="false" screen-reader-focusable="false" drawing-order="3" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false"> <android.widget.FrameLayout index="0" package="com.brother.ptouch.iprintandlabel" class="android.widget.FrameLayout" text="" resource-id="com.brother.ptouch.iprintandlabel:id/option_copies" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[42,685][1038,880]" displayed="true" a11y-important="false" screen-reader-focusable="false" drawing-order="1" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false"> <android.widget.RelativeLayout index="0" package="com.brother.ptouch.iprintandlabel" class="android.widget.RelativeLayout" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[42,685][1038,880]" displayed="true" a11y-important="false" screen-reader-focusable="false" drawing-order="1" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false"> <android.widget.TextView index="0" package="com.brother.ptouch.iprintandlabel" class="android.widget.TextView" text="Copies" resource-id="com.brother.ptouch.iprintandlabel:id/option_title" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[42,727][156,778]" displayed="true" a11y-important="true" screen-reader-focusable="false" drawing-order="1" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false" /> <android.widget.TextView index="1" package="com.brother.ptouch.iprintandlabel" class="android.widget.TextView" text="1" resource-id="com.brother.ptouch.iprintandlabel:id/option_number" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[42,778][786,835]" displayed="true" a11y-important="true" screen-reader-focusable="false" drawing-order="2" showing-hint="false" text-entry-key="false" multiline="true" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" max-text-length="2" content-invalid="false" /> <android.view.View index="2" package="com.brother.ptouch.iprintandlabel" class="android.view.View" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[42,856][786,859]" displayed="true" a11y-important="false" screen-reader-focusable="false" drawing-order="3" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false" /> <android.widget.ImageView index="3" package="com.brother.ptouch.iprintandlabel" class="android.widget.ImageView" text="" resource-id="com.brother.ptouch.iprintandlabel:id/option_down" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[786,754][912,880]" displayed="true" a11y-important="true" screen-reader-focusable="false" drawing-order="4" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false" /> <android.widget.ImageView index="4" package="com.brother.ptouch.iprintandlabel" class="android.widget.ImageView" text="" resource-id="com.brother.ptouch.iprintandlabel:id/option_up" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[912,754][1038,880]" displayed="true" a11y-important="true" screen-reader-focusable="false" drawing-order="5" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false" /> </android.widget.RelativeLayout> </android.widget.FrameLayout> </android.widget.RelativeLayout> <android.widget.RelativeLayout index="3" package="com.brother.ptouch.iprintandlabel" class="android.widget.RelativeLayout" text="" resource-id="com.brother.ptouch.iprintandlabel:id/option_cut_lay" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,880][1080,1075]" displayed="true" a11y-important="false" screen-reader-focusable="false" drawing-order="9" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false"> <android.widget.TextView index="0" package="com.brother.ptouch.iprintandlabel" class="android.widget.TextView" text="Cut Options" resource-id="com.brother.ptouch.iprintandlabel:id/option_cut_title" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[42,922][912,973]" displayed="true" a11y-important="true" screen-reader-focusable="false" drawing-order="1" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false" /> <android.widget.TextView index="1" package="com.brother.ptouch.iprintandlabel" class="android.widget.TextView" text="No setting" resource-id="com.brother.ptouch.iprintandlabel:id/option_cut_options" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="true" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[42,973][912,1030]" displayed="true" a11y-important="true" screen-reader-focusable="false" drawing-order="2" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false" /> <android.view.View index="2" package="com.brother.ptouch.iprintandlabel" class="android.view.View" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[42,1051][912,1054]" displayed="true" a11y-important="false" screen-reader-focusable="false" drawing-order="3" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false" /> <android.widget.ImageView index="3" package="com.brother.ptouch.iprintandlabel" class="android.widget.ImageView" text="" resource-id="com.brother.ptouch.iprintandlabel:id/option_cut_expand" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[912,949][1038,1075]" displayed="true" a11y-important="true" screen-reader-focusable="false" drawing-order="4" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false" /> </android.widget.RelativeLayout> </android.widget.LinearLayout> </android.widget.ScrollView> </android.widget.LinearLayout> </android.widget.FrameLayout> </android.widget.FrameLayout> </androidx.appcompat.widget.LinearLayoutCompat> </android.widget.FrameLayout> </android.widget.FrameLayout> </android.widget.FrameLayout> </android.widget.LinearLayout> </android.widget.FrameLayout> </hierarchy> 这是展开组件后的信息: <?xml version='1.0' encoding='UTF-8' standalone='yes' ?> <hierarchy index="0" class="hierarchy" rotation="0" width="1080" height="2424"> <android.widget.FrameLayout index="0" package="com.brother.ptouch.iprintandlabel" class="android.widget.FrameLayout" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[42,577][912,829]" displayed="true" a11y-important="true" screen-reader-focusable="false" drawing-order="0" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false"> <android.widget.FrameLayout index="0" package="com.brother.ptouch.iprintandlabel" class="android.widget.FrameLayout" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[42,577][912,829]" displayed="true" a11y-important="false" screen-reader-focusable="false" drawing-order="1" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false"> <android.widget.ListView index="0" package="com.brother.ptouch.iprintandlabel" class="android.widget.ListView" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="true" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[42,577][912,829]" displayed="true" a11y-important="true" screen-reader-focusable="false" drawing-order="1" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false"> <android.widget.CheckedTextView index="0" package="com.brother.ptouch.iprintandlabel" class="android.widget.CheckedTextView" text="Normal" resource-id="android:id/text1" checkable="true" checked="true" clickable="true" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[42,577][912,703]" displayed="true" a11y-important="true" screen-reader-focusable="false" drawing-order="1" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false" /> <android.widget.CheckedTextView index="1" package="com.brother.ptouch.iprintandlabel" class="android.widget.CheckedTextView" text="High" resource-id="android:id/text1" checkable="true" checked="false" clickable="true" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[42,703][912,829]" displayed="true" a11y-important="true" screen-reader-focusable="false" drawing-order="2" showing-hint="false" text-entry-key="false" dismissable="false" a11y-focused="false" heading="false" live-region="0" context-clickable="false" content-invalid="false" /> </android.widget.ListView> </android.widget.FrameLayout> </android.widget.FrameLayout> </hierarchy> 展开后的内容可能动态浮层的形式,增加1.写一个函数判断当前是否有浮层存在 2.定位获取新加载的元素并用于点击 3.参考def safe_select_from_spinner(self, spinner_id, target_text): """ 安全选择 Spinner 中的某个选项 """ try: # 1. 点击 Spinner 展开 spinner = self.driver.find_element(AppiumBy.ID, spinner_id) spinner.click() print(f"👉 已点击下拉框: {spinner_id}") # 2. 等待浮层中的选项出现 option_xpath = f"//android.widget.CheckedTextView[@text='{target_text}']" target_option = WebDriverWait(self.driver, 5).until( EC.element_to_be_clickable((AppiumBy.XPATH, option_xpath)) ) # 3. 点击目标选项 target_option.click() print(f"✅ 成功选择: {target_text}") return True except TimeoutException: print(f"❌ 超时:未找到选项 '{target_text}'") return False except Exception as e: print(f"❌ 操作失败: {e}") return False 修改我的现有代码,要求不丢失现有功能 from appium import webdriver from appium.options.android import UiAutomator2Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException, NoSuchElementException, ElementNotInteractableException import time # ==================== 目标应用配置 ==================== TARGET_PACKAGE = "com.brother.ptouch.iprintandlabel" TARGET_ACTIVITY = ".module.home.home.HomeActivity" # 主页 Activity MAX_RESTORE_ATTEMPTS = 2 MAX_HISTORY_REPEAT = 3 # ==================== 配置 Appium Options ==================== def create_driver(): options = UiAutomator2Options() options.set_capability("platformName", "Android") options.set_capability("deviceName", "emulator-5554") options.set_capability("automationName", "UiAutomator2") options.set_capability("noReset", True) options.set_capability("fullReset", False) options.set_capability("autoGrantPermissions", True) options.set_capability("newCommandTimeout", 600) options.set_capability("appPackage", TARGET_PACKAGE) options.set_capability("appActivity", TARGET_ACTIVITY) driver = webdriver.Remote('http://localhost:4723', options=options) return driver class AndroidAppExplorer: def __init__(self, driver): self.driver = driver self.wait = WebDriverWait(driver, 10) self.clicked_identifiers = set() self.recovery_history = [] self.restore_attempt_count = 0 def get_current_state(self): try: current_package = self.driver.current_package current_activity = self.driver.current_activity return current_package, current_activity except: return None, None def is_in_target_app(self): pkg, _ = self.get_current_state() return pkg == TARGET_PACKAGE def wait_for_page_load(self, timeout=10): """等待页面加载完成""" try: WebDriverWait(self.driver, timeout).until( EC.presence_of_element_located(( 'xpath', '//android.view.ViewGroup[@displayed="true"] | ' '//android.widget.FrameLayout[@displayed="true"]' )) ) time.sleep(1) except Exception as e: print(f"⚠️ 页面等待超时或出错: {e}") def ensure_in_target_app(self): current_pkg, current_act = self.get_current_state() if not self.is_in_target_app(): print(f"⚠️ 当前不在目标应用 ({current_pkg}/{current_act}),正在尝试恢复...") current_key = (current_pkg, current_act) recent_entries = self.recovery_history[-10:] if recent_entries.count(current_key) >= MAX_HISTORY_REPEAT: print(f"🛑 已多次回到 {current_key},疑似陷入循环,停止恢复。") return False self.recovery_history.append(current_key) # 方法1:back 返回 if self.restore_attempt_count < MAX_RESTORE_ATTEMPTS: try: self.driver.back() time.sleep(1.5) if self.is_in_target_app(): print("✅ 通过 back 成功返回目标 App") self.restore_attempt_count = 0 self.wait_for_page_load(10) return True except: pass # 方法2:activate_app if self.restore_attempt_count < MAX_RESTORE_ATTEMPTS: try: self.driver.activate_app(TARGET_PACKAGE) print("🔁 正在通过 activate_app 恢复...") time.sleep(2) self.wait_for_page_load(10) if self.is_in_target_app(): print("✅ 成功激活目标 App") self.restore_attempt_count = 0 return True except Exception as e: print(f"❌ activate_app 失败: {e}") # 方法3:冷重启 if self.restore_attempt_count >= MAX_RESTORE_ATTEMPTS: try: print("💀 执行冷重启...") self.driver.terminate_app(TARGET_PACKAGE) time.sleep(2) self.driver.activate_app(TARGET_PACKAGE) time.sleep(5) self.wait_for_page_load(10) if self.is_in_target_app(): print("✅ 冷重启成功") self.restore_attempt_count = 0 return True except Exception as e: print(f"❌ 冷重启失败: {e}") self.restore_attempt_count += 1 if self.restore_attempt_count > 5: print("🛑 恢复尝试过多,终止运行") return False return True else: self.recovery_history.append((current_pkg, current_act)) self.wait_for_page_load(1) return True def get_element_identifier(self, element): try: attrs = [ element.get_attribute("resource-id") or "", element.get_attribute("text") or "", element.get_attribute("content-desc") or "", element.tag_name or "" ] return "/".join([a for a in attrs if a.strip()]) except: return "unknown_element" def find_clickable_elements(self): xpath = """ //*[(@clickable='true' or @focusable='true') and @displayed='true' and @enabled='true'] """ try: elements = self.driver.find_elements('xpath', xpath) return [e for e in elements if e.is_displayed()] except Exception as e: print(f"❌ 查找元素失败: {e}") return [] def classify_elements(self, elements): bottom_nav_container = "com.brother.ptouch.iprintandlabel:id/bottomNavigationView" nav_ids = [ "com.brother.ptouch.iprintandlabel:id/navigation_my_label", "com.brother.ptouch.iprintandlabel:id/navigation_setting", "com.brother.ptouch.iprintandlabel:id/navigation_buy" ] nav_elements = [] non_nav_elements = [] for elem in elements: try: res_id = elem.get_attribute("resource-id") or "" content_desc = elem.get_attribute("content-desc") or "" text = elem.get_attribute("text") or "" if (res_id == bottom_nav_container or res_id in nav_ids or content_desc in ["My Labels", "Settings", "Shop"]): nav_elements.append(elem) else: non_nav_elements.append(elem) except Exception as e: print(f"⚠️ 分类元素时异常: {e}") continue return nav_elements, non_nav_elements def handle_alert_popup(self): alert_xpath = "//*[contains(@text, 'OK') or contains(@text, '允许') or contains(@text, 'Allow')]" try: button = self.driver.find_element('xpath', alert_xpath) if button.is_displayed(): button.click() print("✅ 自动处理了弹窗") time.sleep(1) except: pass def is_interactive_input(self, element): """判断是否为可视且可交互的输入框""" try: if not element.is_displayed(): return False focusable = element.get_attribute("focusable") == "true" enabled = element.get_attribute("enabled") == "true" clazz = element.get_attribute("className") or "" is_edit_type = any(kw in clazz for kw in ["EditText", "TextInput"]) return focusable and enabled and is_edit_type except: return False def handle_input_field(self, input_elem): """处理输入框:显示提示、当前值,并让用户手动输入后读取结果""" hint = input_elem.get_attribute("hint") or "null" current_text = input_elem.get_attribute("text") or "" print(f"\n🔍 发现输入框") if hint != "null": print(f" 提示: '{hint}'") if current_text: print(f" 当前文本: '{current_text}'") print("请在设备上手动输入内容(3秒内完成)...") time.sleep(3) try: entered_value = input_elem.get_attribute("text") or "" # ✅ 使用 text 而非 value except: entered_value = "" print(f"📝 用户在输入框中输入的内容为: '{entered_value}'") def click_element_safely(self, element): try: identifier = self.get_element_identifier(element) if identifier in self.clicked_identifiers: return False try: self.driver.execute_script("arguments[0].scrollIntoView(true);", element) except: pass time.sleep(0.5) element.click() print(f"🟢 点击成功: {identifier}") self.clicked_identifiers.add(identifier) time.sleep(1.5) return True except Exception as e: print(f"🔴 点击失败 {self.get_element_identifier(element)}: {str(e)}") return False def explore_page(self): print("\n🔄 开始探索当前页面...") while True: if not self.ensure_in_target_app(): print("❌ 无法恢复至目标应用,退出。") break self.handle_alert_popup() elements = self.find_clickable_elements() if not elements: print("📭 当前页面无可点击元素。") try: self.driver.back() print("🔙 返回上一页...") time.sleep(2) continue except: break nav_elements, non_nav_elements = self.classify_elements(elements) print(f"📌 导航栏元素数量: {len(nav_elements)}, 非导航栏元素数量: {len(non_nav_elements)}") # === 第一阶段:优先点击导航栏元素 === navigated = False for elem in nav_elements: if self.is_interactive_input(elem): self.handle_input_field(elem) continue if self.click_element_safely(elem): navigated = True break if navigated: continue # === 第二阶段:点击非导航栏元素 === any_clicked = False for elem in non_nav_elements: if self.is_interactive_input(elem): self.handle_input_field(elem) continue if self.click_element_safely(elem): any_clicked = True break if not any_clicked: print("✅ 当前页面所有可点击元素已处理完毕。") try: self.driver.back() print("🔙 返回上一页继续探索...") time.sleep(2) except: print("🔚 无法返回,探索结束。") break time.sleep(1) def run(self): print("🚀 启动 Android 应用探索器...") try: self.explore_page() except KeyboardInterrupt: print("\n👋 用户中断执行。") except Exception as e: print(f"💥 执行过程中发生错误: {type(e).__name__}: {e}") finally: print("🔚 自动化结束。") # =============== 主程序入口 =============== if __name__ == "__main__": driver = create_driver() explorer = AndroidAppExplorer(driver) try: explorer.run() finally: driver.quit()
10-17
package com.tplink.mercusys.component.onboarding.adapter import android.content.Context import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import com.tplink.apps.design.resource.setCardBackground import com.tplink.design.list.TPSingleLineItemViewHolder import com.tplink.design.util.accessibility.initAccessDelegate import com.tplink.design.util.accessibility.mergeStringsForA11y import com.tplink.libtpnetwork.Utils.GsonUtils import com.tplink.libtputility.log.TLog import com.tplink.mercusys.R import com.tplink.mercusys.component.account.login.bean.RegionCode import com.tplink.mercusys.component.onboarding.bean.dsl.Region import com.tplink.mercusys.databinding.ItemRegionListLetterTitleBinding import java.text.Collator import java.util.Collections class ChooseRegionAdapter( val context: Context, regionCodeList: List<Region>, regionCode: Region? ) : RecyclerView.Adapter<RecyclerView.ViewHolder>() { companion object { const val REGION = 0 const val TITLE = 1 } private var regionList: List<Region> = regionCodeList var selectRegion: Region? = regionCode private var lastChoose: Int = -1 var callback: RegionItemCallback? = null private var selectPosition = -1 private val LETTER_ARRAY = arrayOf( "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" ) private val titlePosition = IntArray(LETTER_ARRAY.size) { -1 } private var regionPosition = IntArray(regionCodeList.size) private val showList: MutableList<AdapterItem> = ArrayList() private val regionPositionToAdapterPositionMap: HashMap<Int, Int> = HashMap() private val regionGroupByLetterCountMap: HashMap<String, Int> = HashMap() init { initRegionList() formatMessageContainTitle() } override fun getItemCount(): Int { return showList.size } fun getTargetLetterRegionFirstIndex(title: String?): Int { for (i in LETTER_ARRAY.indices) { if (LETTER_ARRAY[i].equals(title, ignoreCase = true)) { return titlePosition[i] } } return -1 } fun getCurrentTitleLetter(title: String?): String? { val currentIndex = getTitlePosition(title) return LETTER_ARRAY.getOrNull(currentIndex) } fun getPreviousTitleLetter(title: String?): String? { var currentIndex = getTitlePosition(title) if (currentIndex == -1) { return null } while (currentIndex - 1 >= 0) { currentIndex -= 1 if (titlePosition[currentIndex] != -1) { return LETTER_ARRAY[currentIndex] } } return null } fun getNextTitleLetter(title: String?): String? { var currentIndex = getTitlePosition(title) if (currentIndex == -1) { return null } while (currentIndex + 1 < LETTER_ARRAY.size) { currentIndex += 1 if (titlePosition[currentIndex] != -1) { return LETTER_ARRAY[currentIndex] } } return null } fun getTitleExitsList(): Map<String, Boolean> { val map = hashMapOf<String, Boolean>() titlePosition.forEachIndexed { index, i -> map[LETTER_ARRAY[index]] = (i != -1) } return map } fun changeItem() { notifyItemChanged(regionList.indexOf(selectRegion)) if (lastChoose >= 0 && lastChoose < showList.size) { notifyItemChanged(lastChoose) } lastChoose = regionList.indexOf(selectRegion) } fun replaceData(regionCodeList: List<Region>, select: Region) { regionList = regionCodeList regionPosition = IntArray(regionCodeList.size) selectRegion = select initRegionList() formatMessageContainTitle() notifyDataSetChanged() } fun getAdapterPositionByRegionPosition(position: Int): Int { return regionPositionToAdapterPositionMap[position] ?: 0 } interface RegionItemCallback { fun onItemClick(position: Int) } override fun getItemViewType(position: Int): Int { return if (showList[position].isTitle) { 1 } else super.getItemViewType(position) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { return when (viewType) { REGION -> { TPSingleLineItemViewHolder.create(parent) } else -> { LetterViewHolder( ItemRegionListLetterTitleBinding.inflate( LayoutInflater.from(parent.context), parent, false ) ) } } } override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { when (holder.itemViewType) { REGION -> onBindSelectGameViewHolder( holder as TPSingleLineItemViewHolder, position ) TITLE -> onBindLetterViewHolder( holder as LetterViewHolder, position ) } } private fun getTitlePosition(title: String?): Int { return LETTER_ARRAY.indexOfFirst { title?.startsWith(it, ignoreCase = true) == true } } private fun onBindLetterViewHolder(holder: LetterViewHolder, position: Int) { holder.binding.letterTv.text = showList[position].letter } private fun onBindSelectGameViewHolder(holder: TPSingleLineItemViewHolder, position: Int) { val regionPosition = showList[position].num val region: Region = regionList.getOrNull(regionPosition) ?: return holder.lineItem.setEndIcon(R.drawable.ic_nvi_done) if (region.regionName == "GERMANY") { TLog.d("TestApp", "region: " + GsonUtils.getUnifiedGson().toJson(region)) } if (position == 1) { TLog.d("TestApp", "selectRegion: " + GsonUtils.getUnifiedGson().toJson(selectRegion)) } if (selectRegion == null && regionPosition == 0) { holder.lineItem.getEndIcon().visibility = View.VISIBLE holder.lineItem.contentDescription = mergeStringsForA11y(context.getString(R.string.common_selected), region.regionName) selectRegion = region lastChoose = holder.adapterPosition callback?.onItemClick(lastChoose) } else if (region == selectRegion) { lastChoose = holder.adapterPosition callback?.onItemClick(lastChoose) holder.lineItem.getEndIcon().visibility = View.VISIBLE holder.lineItem.contentDescription = mergeStringsForA11y(context.getString(R.string.common_selected), region.regionName) } else { holder.lineItem.getEndIcon().visibility = View.INVISIBLE holder.lineItem.contentDescription = region.regionName } val rc = RegionCode.fromSymbol(region.regionCode) if (rc != null && rc.resId != -1) { holder.lineItem.setTitleText(RegionCode.fromSymbol(region.regionCode).resId) } else { holder.lineItem.getTitle().text = region.regionName } val firstLetter = getRegionFirstString(regionPosition) val firstLetterPosition = getTitlePosition(firstLetter) + 1 val groupCount = regionGroupByLetterCountMap[firstLetter] ?: 0 holder.lineItem.showDivider(position != firstLetterPosition + groupCount - 1) holder.setCardBackground(position - firstLetterPosition, groupCount) holder.itemView.setOnClickListener { selectRegion = regionList.getOrNull(regionPosition) ?: return@setOnClickListener notifyItemChanged(holder.adapterPosition) if (lastChoose >= 0 && lastChoose < showList.size) { notifyItemChanged(lastChoose) } lastChoose = holder.adapterPosition } holder.itemView.initAccessDelegate().markAsButton() .setContentDescriptionProvider { holder.lineItem.getTitle().text.toString() } .setSelectedProvider { holder.lineItem.getEndIcon().visibility == View.VISIBLE } } private fun initRegionList() { if (regionList.isNotEmpty()) { val coll = Collator.getInstance(context.resources.configuration.locale) coll.strength = Collator.PRIMARY Collections.sort( regionList ) { o1: Region, o2: Region -> coll.compare( o1.regionName, o2.regionName ) } } } private fun formatMessageContainTitle() { showList.clear() regionGroupByLetterCountMap.clear() var isTitle = true var currentTitle = "A" var letterNum = 0 var listNum = 0 var groupCount = 0 for (i in regionList.indices) { if (isTitle) { regionGroupByLetterCountMap[currentTitle] = groupCount groupCount = 0 currentTitle = getRegionFirstString(i) val titleItem = AdapterItem() titleItem.isTitle = true titleItem.letter = currentTitle isTitle = false showList.add(titleItem) while (letterNum < LETTER_ARRAY.size && !LETTER_ARRAY[letterNum] .equals(currentTitle, ignoreCase = true) ) { // 如果某个首字母不存在,保持titlePosition值为-1 letterNum++ } if (letterNum < LETTER_ARRAY.size) { titlePosition[letterNum] = listNum letterNum++ } listNum++ } val item = AdapterItem() item.isTitle = false item.num = i if (i != regionList.size - 1) { if (!getRegionFirstString(i).equals( getRegionFirstString(i + 1), ignoreCase = true ) ) { isTitle = true } } if (regionList[i].isSelect) { selectPosition = listNum } regionPositionToAdapterPositionMap[i] = showList.size showList.add(item) groupCount++ regionPosition[i] = listNum listNum++ } regionGroupByLetterCountMap[currentTitle] = groupCount while (letterNum < LETTER_ARRAY.size) { titlePosition[letterNum] = showList.size - 1 letterNum++ } } private fun getRegionFirstString(index: Int): String { if (index >= regionList.size) { return "" } val text = regionList[index].regionName return if (text == null || text.isEmpty()) { "" } else Region.stripAccents(text.substring(0, 1)) } internal class LetterViewHolder(val binding: ItemRegionListLetterTitleBinding) : RecyclerView.ViewHolder(binding.root) internal class AdapterItem { var isTitle = false var num = 0 var letter = "" } } 传进来的regionList 最终转变成 showList是什么样的格式
11-21
内容概要:本文详细介绍了一种基于Simulink的表贴式永磁同步电机(SPMSM)有限控制集模型预测电流控制(FCS-MPCC)仿真系统。通过构建PMSM数学模型、坐标变换、MPC控制器、SVPWM调制等模块,实现了对电机定子电流的高精度跟踪控制,具备快速动态响应和低稳态误差的特点。文中提供了完整的仿真建模步骤、关键参数设置、核心MATLAB函数代码及仿真结果分析,涵盖转速、电流、转矩和三相电流波形,验证了MPC控制策略在动态性能、稳态精度和抗负载扰动方面的优越性,并提出了参数自整定、加权代价函数、模型预测转矩控制和弱磁扩速等优化方向。; 适合人群:自动化、电气工程及其相关专业本科生、研究生,以及从事电机控制算法研究与仿真的工程技术人员;具备一定的电机原理、自动控制理论和Simulink仿真基础者更佳; 使用场景及目标:①用于永磁同步电机模型预测控制的教学演示、课程设计或毕业设计项目;②作为电机先进控制算法(如MPC、MPTC)的仿真验证平台;③支撑科研中对控制性能优化(如动态响应、抗干扰能力)的研究需求; 阅读建议:建议读者结合Simulink环境动手搭建模型,深入理解各模块间的信号流向与控制逻辑,重点掌握预测模型构建、代价函数设计与开关状态选择机制,并可通过修改电机参数或控制策略进行拓展实验,以增强实践与创新能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值