android - 如何在不同的布局中获取 id

标签 android android-layout

当我使用 findVewById 时,我在 single_item.xml 中有 id "@+id/call" (布局 setcontextview (R.layout.main)) .app crash .how to fix the error

最佳答案

如果你想访问另一个布局(不是 Activity 布局)中的 View ,那么你可以扩展你想使用的布局并以这种方式访问​​它。

例子:

View inflatedView = getLayoutInflater().inflate(R.layout.other_layout, null);
TextView text = (TextView) inflatedView.findViewById(R.id.text_view);
text.setText("Hello!");

可以找到有关膨胀布局的更多信息here .

关于android - 如何在不同的布局中获取 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4211189/

相关文章:

javascript - 我可以清除移动设备上触摸的 div 的焦点吗?

java - 使用数组设置一周的第一天

android - 在支持 v8 的情况下使用 Renderscript api11+ 时出错

android - 造型 Yuri Kanivets 的 WheelView

android - Material 设计步进控制

android - 在 Android/Eclipse 上使用 3 个 MediaPlayer 对象同步播放音乐

android - 每次打开应用程序时显示 GCM 消息后的 AlertDialog

java - 有没有办法检查概览按钮?

android - 如何为 Kindle Fire 配置 Android 模拟器?

java - 尝试制作一个圆形布局,其中的元素以不同的距离围绕中心定位