我无法以编程方式设置LinearLayout参数。我尝试的一切,我得到了这个例外。
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()的布局。
<?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>
答案 0 :(得分:0)
视图的getLayoutParams()
调用的返回类型不依赖于视图的类型,而是取决于该视图的父级的类型。因此,即使outerLayout
是LinearLayout
,其布局参数也可以是任何内容。
您的outerLayout
视图似乎是在某种ListView
内托管的,这就是outerLayout.getLayoutParams()
的结果属于AbsListView.LayoutParams
类型的原因。