我在我的安卓项目中使用了一个bottomSheetBehavior。见下文代码:
onlineGame.java:
// get the bottom sheet view
ConstraintLayout llBottomSheet = findViewById(R.id.end_of_online_game_bottom_sheet_behavior_cl);
// init the bottom sheet behavior
end_of_online_game_popup = BottomSheetBehavior.from(llBottomSheet);avtivity_online_game.xml:
.
.
.
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.androidsample.BottomSheetActivity">
<!-- include bottom sheet -->
<include
android:id="@+id/includeBottomSheetBehavior"
layout="@layout/test_end_of_online_game_popup" />
</android.support.design.widget.CoordinatorLayout>
.
.
.test_end_of_online_game_popup.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/end_of_online_game_bottom_sheet_behavior_cl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/cardview_light_background"
android:visibility="gone"
app:behavior_hideable="false"
app:behavior_peekHeight="120dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
.
.
.问题是这条线:
ConstraintLayout llBottomSheet = findViewById(R.id.end_of_online_game_bottom_sheet_behavior_cl);返回null。我甚至将代码位置更改为onResume,但没有工作。当我在test_end_of_online_game_popup中获得另一个元素时,它工作得很好,而不是null。
有什么问题吗?
tnx
发布于 2018-09-16 07:50:34
两种选择:
ConstraintLayout llBottomSheet = findViewById(R.id. includeBottomSheetBehavior);<include layout="@layout/test_end_of_online_game_popup" />发布于 2018-09-16 07:50:14
当您使用包含的布局时:
使用您给包含标记的id的find视图。
findViewById(R.id.includeBottomSheetBehavior)或者,您可以在布局标签中省略id,这样它就不会被覆盖。
在< include >标记中,只需要布局属性。此属性是对希望包含的布局文件的引用。此标记还允许您覆盖包含的布局的一些属性。
上面的示例显示您可以使用android:id来指定所包含布局的根视图的id;如果定义了,它也将覆盖包含的布局的id。同样,您可以覆盖所有布局参数。
来源:http://www.curious-creature.com/2009/02/25/android-layout-trick-2-include-to-reuse/
发布于 2018-09-16 08:07:25
试试这个:
View view = findViewById(R.id.includeBottomSheetBehavior);//firstly get the root view ID
ConstraintLayout llBottomSheet = view.findViewById(R.id.end_of_online_game_bottom_sheet_behavior_cl);https://stackoverflow.com/questions/52352030
复制相似问题