android - LinearLayout 无法转换为 CoordinatorLayout

标签 android xml android-activity android-linearlayout

我有一个问题。我想以编程方式设置边距参数,但它不起作用。 我有这个 xml 样式表:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">


</android.support.design.widget.AppBarLayout>


<ImageView
    android:layout_width="1440px"
    android:layout_height="1000px"
    android:id="@+id/post_view_photo"
    android:transitionName="@string/transition_image"/>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|start"
    android:layout_margin="@dimen/fab_margin"
    app:layout_anchor="@+id/post_view_photo"
    android:src="@android:drawable/stat_notify_chat"
    app:layout_anchorGravity="bottom|end" />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_anchor="@+id/post_view_photo"
    app:layout_anchorGravity="bottom|start">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text_bar"
        android:background="@color/colorPrimary">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/username_text"
            android:layout_gravity="center_vertical|start"
            app:layout_anchor="@+id/post_view_photo"
            app:layout_anchorGravity="bottom|start"
            android:textSize="25dp"
            android:transitionName="@string/transition_username"/>
    </LinearLayout>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginTop="808px"
    android:id="@+id/view_bottom_layout">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:id="@+id/post_user_image"
                android:src="@drawable/back"
                android:paddingTop="10px"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/post_view_maintext"/>
        </LinearLayout>

    </LinearLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#414141"/>
</LinearLayout>

我想以编程方式设置边距。但我收到此错误:

java.lang.ClassCastException: android.support.design.widget.CoordinatorLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams

我在我的 Activity 中这样做:

LinearLayout bottomLayout = (LinearLayout)findViewById(R.id.view_bottom_layout);
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) bottomLayout.getLayoutParams();
                        params.setMargins(0,imageView.getHeight(),0,0);
                        bottomLayout.setLayoutParams(params);

我可以做什么来解决这个问题?我没有发现任何对我有帮助的东西。谢谢!

最佳答案

LayoutParams 始终与您找到的布局的父布局相关联。

由于问题中的布局文件被剪裁,我根据输出假设 view_bottom_layout 位于 CoordinationLayout 内。

如果是这样,您需要 CooperativeLayout 的参数并设置它们:

CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) bottomLayout.getLayoutParams();
params.setMargins(0,imageView.getHeight(),0,0);
bottomLayout.setLayoutParams(params);

关于android - LinearLayout 无法转换为 CoordinatorLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38620360/

相关文章:

android - 如何从主要父 Activity 以外的 Activity 启动 fragment ?

java - Html.ImageGetter TextView

ruby - 如何在 ruby​​ 中进行 xml 签名

android - 如何在 fragment 中使用 onNewIntent(Intent intent) 方法?

xml - 选择完整的树并仅选择某些子元素

java - 如何将 XML 输入流解析为字符串或对象 (Android studio)

android - rxjava android retryWhen 在上次尝试时没有调用 onError

android - android 中的 java.io.FileNotFoundException

java - 我应该用对象类型为我的变量添加后缀吗?

android - 如何在 Jenkins 中创建具有特定风格的 .apk?