Android CoordinatorLayout显然具有FrameLayout参数
当我试图通过对CoordinatorLayout的适当回调来设置BottomSheetBehavior时,我偶然发现了这一点。当获取所述CoordinatorLayout的布局参数时,我会继续接收FrameLayout参数。这是代码方面的情况 从底层对话框片段实现Android CoordinatorLayout显然具有FrameLayout参数,android,kotlin,Android,Kotlin,当我试图通过对CoordinatorLayout的适当回调来设置BottomSheetBehavior时,我偶然发现了这一点。当获取所述CoordinatorLayout的布局参数时,我会继续接收FrameLayout参数。这是代码方面的情况 从底层对话框片段实现 override fun setupDialog(dialog: Dialog?, style: Int) { super.setupDialog(dialog, style) val binding = DataBi
override fun setupDialog(dialog: Dialog?, style: Int) {
super.setupDialog(dialog, style)
val binding = DataBindingUtil.inflate<AmbientDialogBinding>(LayoutInflater.from(activity), R.layout.ambient_dialog, null, false)
val params = activity?.findViewById<CoordinatorLayout>(R.id.homeCoordinatorLayout)?.layoutParams as CoordinatorLayout.LayoutParams
val behaviour = params.behavior
if (behaviour != null && behaviour is BottomSheetBehavior) {
behaviour.setBottomSheetCallback(behaviorCallback)
}
}
覆盖对话框(对话框:dialog?,样式:Int){
super.setupDialog(对话框,样式)
val binding=DataBindingUtil.充气(LayoutFlater.from(活动),R.layout.ambient_对话框,null,false)
val params=活动?.findViewById(R.id.homeCoordinatorLayout)?.layoutParams作为CoordinatorLayout.layoutParams
val行为=参数行为
if(behavior!=null&&behavior为behavior){
behavior.setBottomSheetCallback(behavior callback)
}
}
main_activity.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/homeCoordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/lightGray"
app:layout_behavior="@string/bottom_sheet_behavior"
tools:context=".HomeActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:theme="@style/Base.ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/addAmbientButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginBottom="8dp"
android:clickable="true"
android:focusable="true"
app:backgroundTint="@color/colorPrimary"
app:srcCompat="@drawable/ic_add_white_24dp" />
</android.support.design.widget.CoordinatorLayout>
fragment_dialog.xml
<android.support.v7.widget.LinearLayoutCompat
android:id="@+id/ambientDialogTextLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:orientation="vertical">
<EditText
android:id="@+id/ambientIdField"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:gravity="center"
android:hint="Identificativo Ambiente"
android:text="@={vm.UIDLive}"
android:textSize="16sp" />
<EditText
android:id="@+id/ambientNameField"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:gravity="center"
android:hint="Nome Ambiente"
android:text="@={vm.nameLive}"
android:textSize="16sp" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/confirmAmbientButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:clickable="true"
android:focusable="true"
app:backgroundTint="@color/colorPrimary"
app:srcCompat="@drawable/ic_check_white_24dp" />
</android.support.v7.widget.LinearLayoutCompat>
我在主活动xml中使用的框架布局用于另一个片段,我认为这与问题无关。希望有人能帮我。提前谢谢 我还没有确认这一点,但我相信您传递给
setContentView()
(或膨胀的布局分辨率)的视图已添加到FrameLayout中
视图的LayoutParams由其父视图定义,而不是由它定义。因此,如果在ConstraintLayout中有一个LinearLayout,则调用LinearLayoutStance.layoutParams
将返回ConstraintLayout.layoutParams的实例
不过,我确实知道,setContentView()
会将布局添加到视图组中。可能无法保证根布局是FrameLayout。您不应该在根视图上强制转换getParams()
,而应该依赖ViewGroup.LayoutParams的默认返回值
编辑:
您可以在此处看到可能的布局列表:
com.android.internal.R.content
是根目录的ID
查看该列表和相应的XML文件,下面列出了可能的根布局类型:
->R.layout.screen\u swipe\u disease
com.android.internal.widget.SwipeDismissLayout
->R.layout.screen\u title\u icons
FrameLayout
->R.layout.screen\u进度
FrameLayout
->R.layout.screen\u custom\u title
FrameLayout
->R.layout.screen\u action\u bar
FrameLayout
->R.layout.screen\u标题
FrameLayout
->R.layout.screen\u simple\u overlay\u action\u mode
FrameLayout
->R.layout.screen\u simple
FrameLayout
您甚至可以使用
对话框CustomTitleDecorLayout
属性定义自己的属性(尽管这对应用程序不可用)。这也仅来自最新的AOSP源代码。在三星、华为等公司或其他版本的Android上可能不一样。对不起,您可以更好地解释一下:“您不应该在根视图上强制转换getParams(),而应该依赖ViewGroup.LayoutParams的默认返回值。”。我不明白我该做什么。您正在谈论ViewGroup,那么我如何在setupDialog上引用它呢?无论如何,非常感谢您在这么短的时间内回答!抱歉,语法和方法错误。您有layoutParams作为CoordinatorLayout.layoutParams
。删除作为CoordinatorLayout.LayoutParams的。但是,我无法访问行为,因为通用LayoutParams没有它。我是否应该从BottomSheetBehavior使用from()方法?但是,我还是认为它不起作用,因为在函数中,它检查传递的视图的layoutparams是否为CoordinatorLayout.LayoutParamsBehavior应用于子视图,而不是CoordinatorLayout本身。