android - 如何识别当前正在运行的布局 xml?

标签 android android-layout setcontentview

我必须布局名为 activity_layout_1 和 activity_layout_2 的 xml 以及一个公共(public) Activity 。 根据不同的情况,我设置了两个xml的contentview 在某些情况下,我需要检查当前设置的布局。

我可以得到如下的id:

View currentView = this.getWindow().getDecorView().findViewById(android.R.id.content);
        int id = currentView.getId();
            

是否有更好的方法来获取/识别当前正在运行的布局?

最佳答案

那个id是android.R.id.content的id不是当前layout的id。
更好的方法是使用标签 setTag()getTag()

喜欢:

View view = inflater.inflate(R.layout.fragment, container, false);
view.setTag(R.layout.fragment)

关于android - 如何识别当前正在运行的布局 xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73011096/

相关文章:

ios - 如何在 UIScrollView 到达底部时停止滚动

android - 自定义 View 不会添加到预定义的 XML 布局中

android - 布局中其他 View 隐藏的 Material 波纹效果

android - 在 ScrollView 中嵌套 RecyclerViews 或 RecyclerView?

android - 找出屏幕上当前显示的是哪个布局文件

java - 如何在另一个方法中设置setContentView?

java - 如何创建通用方法来容纳任意数量的 POJO

android - 检测到应用原生库的问题

android - 从字节数组创建 typeFace

android - 通过命令行使用Gradle构建Android应用程序时出错