MonoDroid: Iterate over the controls in an Activity?
-
25-10-2019 - |
문제
In Activity.OnContentChanged, I would like to iterate over all of the controls to look get their Tags.
I assume it has something to do with base.Window.HasChildren, but I can't figure out how to actually get the children.
EDIT: ViewGroup, the base class for most layouts, has GetChildCount and GetChildAt. But I still can't figure out how to get from the Activity to that root-level layout.
해결책 2
var viewGroup = this.Window.DecorView as ViewGroup;
if(viewGroup != null){
for (int i = 0; i < view.ChildCount; i++)
{
var control = view.GetChildAt(i);
}
}
다른 팁
You can assign an ID to the root element of your layout and access it through that:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="1"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="2"/>
</LinearLayout>
Then in your activity you could do something like:
var root = FindViewById<LinearLayout>(Resource.Id.Root);
for (int i = 0; i < root.ChildCount; i++)
{
Console.WriteLine(root.GetChildAt(i).Id);
}
제휴하지 않습니다 StackOverflow