i'm writing a demo and find a wired thing:
a created view(
new View(context)
and thenparent.addView(childView)
) can't be saw in Layout Inspector without filter system-defined layers option checked; but the inflated view(layoutInflater.inflate(R.layout.xxx, null)
) can.
i tried to ask Google Gemini and the gemini told me:
When you create a view programmatically (e.g., new TextView(context)) and add it to your Activity's view hierarchy, it might be added to a system-managed layer, especially if you're adding it to a container that is itself part of the system's UI (e.g., a Dialog, PopupWindow, or a custom view that interacts with system UI elements). This isn't always the case, but it's a possibility.
but this answer looks right but useless(did't specify the deeper reason), so i'm here for help, and here's the code i wrote
// can't be shown in hierarchy
val tempTextViewCompat = AppCompatTextView(this).apply {
layoutParams = LinearLayoutCompat.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
text = "Hello World"
textSize = 30f
}
mainView.addView(tempTextViewCompat)
// can be shown in hierarchy
val inflateView = layoutInflater.inflate(R.layout.temp_text, null)
mainView.addView(inflateView)