首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AbsListView$LayoutParams不能强制转换为LinearLayout$LayoutParams

AbsListView$LayoutParams不能强制转换为LinearLayout$LayoutParams
EN

Stack Overflow用户
提问于 2017-12-06 02:13:48
回答 1查看 78关注 0票数 0

我只是不能以编程方式设置LinearLayout参数。无论我尝试什么,我都会得到一个确切的异常。

代码语言:javascript
复制
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) outerLayout.getLayoutParams();
params.bottomMargin = 50;
outerLayout.setLayoutParams(params);


java.lang.ClassCastException: android.widget.AbsListView$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams

我不知道为什么,但似乎我就是不能让LinearLayout.LayoutParams出现在视图中。outerLayout是我的LinearLayout,我正在用findViewById()实例化它。我尝试过导入不同的东西,但没有成功。

下面是我的xml,它表示列表视图中的一项。outer_item_layout是我在其上调用getLayoutParams()的布局。

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/outer_item_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/modeName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_weight="3"
            android:text="Medium Text"
            android:textSize="18dp" />

        <ImageView
            android:id="@+id/dropdown_speed"
            android:layout_width="23dp"
            android:layout_height="23dp"
            android:layout_gravity="center"
            android:layout_marginRight="20dp"
            android:background="?selectableItemBackgroundBorderless"
            android:clickable="true"
            android:src="@drawable/ic_dropdown"
            tools:ignore="InvalidId" />
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:id="@+id/expandable_seekbar"
    android:layout_width="match_parent"
    android:layout_marginBottom="-50dip"
    android:visibility="gone"
    android:layout_height="50dip"
    android:orientation="horizontal">

    <SeekBar
        android:id="@+id/custom_mode_speed_seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp" />

</LinearLayout>

EN

回答 1

Stack Overflow用户

发布于 2017-12-06 02:26:43

视图的getLayoutParams()调用的返回类型不依赖于视图的类型,而是依赖于该视图的父的类型。所以,即使outerLayout是一个LinearLayout,它的布局参数可以是任何东西。

您的outerLayout视图似乎托管在某种ListView中,这就是为什么outerLayout.getLayoutParams()的结果是AbsListView.LayoutParams类型的原因。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47660141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档