Law*_*nez 12 android exception
我试图根据a设置FrameLayout宽度和高度Bitmap,我在下面做了什么
Bitmap theBitmap = BitmapFactory.decodeFile(theFileImage.toString());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(theBitmap.getWidth(), theBitmap.getHeight());
frame.setLayoutParams(lp);
image.setLayoutParams(lp);
image.setImageBitmap(theBitmap);
Run Code Online (Sandbox Code Playgroud)
但是我得到了一个ClassCastException.
我做错了什么?
编辑:
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
Run Code Online (Sandbox Code Playgroud)
Eld*_*abu 15
要设置布局参数,您需要使用其父级的内部类LayoutParams.
例如:如果在RelativeLayout中有一个LinearLayout,并且如果需要设置Linear Layout的布局参数,则需要使用RelativeLayout的LayoutParams内部类.否则它会产生ClassCastException.
因此,在您的情况下,要设置FrameLayout的Layoutparams,您需要使用其父布局的布局参数.假设你的布局是这样的:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:id="@+id/flContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
代码:
FrameLayout frame=(FrameLayout) findViewById(R.id.flContainer);
ImageView image=(ImageView) findViewById(R.id.image);
Bitmap theBitmap = BitmapFactory.decodeFile(theFileImage.toString());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(theBitmap.getWidth(), theBitmap.getHeight());
frame.setLayoutParams(lp);
image.setImageBitmap(theBitmap);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22421 次 |
| 最近记录: |