【发布时间】:2017-12-05 18:13:48
【问题描述】:
我只是无法以编程方式设置 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>
【问题讨论】:
-
发布您的布局 xml。
-
你为什么要以编程方式设置下边距?
-
如果确实需要,那么您可以在根 LinearLayout 中添加另一个 LinearLayout 容器并对其进行操作。
标签: android android-layout android-linearlayout layoutparams