我正在尝试使整个屏幕可点击,所以我正在尝试为布局设置id并在java类中找到它。当我这样做时,它表示不兼容的类型(布局与视图)。我知道它们是不同的类型,但在谷歌搜索时,有几篇帖子建议以这种方式选择活动中的布局。他们可以拨打电话 -
RelativeLayout theLayout = (RelativeLayout) this.findViewById(R.id.Layout)
他们似乎没有收到此错误。
另一个stackoverflow帖子 - onTouchListener for entire screen
我的代码引用它位于 -
之下<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:id="@+id/thelayout" >
<TextView android:text="This will change with speech" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/textview"/>
</RelativeLayout>
活动 -
RelativeLayout theLayout = this.findViewById(R.id.thelayout);
我错过了什么吗?
答案 0 :(得分:3)
而不是这个,
RelativeLayout theLayout = this.findViewById(R.id.thelayout)
在您的活动
中尝试此操作RelativeLayout theLayout = (RelativeLayout) this.findViewById(R.id.thelayout)
答案 1 :(得分:1)
不要将setContentView(int layoutResID)
与布局ID一起使用,而是通过布局获取视图参考,然后使用setContentView(View view)
。
View layout = View.inflate(context, R.layout.theLayout, null)
setContentView(layout)
您不需要将布局强制转换为RelativeLayout,因为您可以将Click / touch侦听器添加到View类。