Android 根据Bitmap设置FrameLayout的高度和宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12192667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set Height and Width of FrameLayout based on Bitmap
提问by Law Gimenez
I'm trying to set the FrameLayout
's width and height based on a Bitmap
, what I did was below
我正在尝试根据 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);
but I'm getting a ClassCastException
.
但我得到了一个ClassCastException
.
What did I do wrong?
我做错了什么?
Edited:
编辑:
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
回答by Eldhose M Babu
For setting Layout params, you need to use the inner class LayoutParams of its parent.
要设置 Layout 参数,您需要使用其父级的内部类 LayoutParams。
For Example : If you are having a LinearLayout inside RelativeLayout and if you need to set the layout params of Linear Layout, you need to use the LayoutParams inner class of RelativeLayout. Else it will gives a ClassCastException.
例如:如果你在RelativeLayout里面有一个LinearLayout,如果你需要设置LinearLayout的布局参数,你需要使用RelativeLayout的LayoutParams内部类。否则它会给出一个 ClassCastException。
So in your case, for setting the FrameLayout's Layoutparams , you need to use its parent Layout's Layout Params. Suppose if your layout is like :
因此,在您的情况下,要设置 FrameLayout 的 Layoutparams ,您需要使用其父布局的 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>
Code :
代码 :
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);
回答by Arif Nadeem
Seeing the ClassCastException
I assume you are doing something illegal here, a couple of questions, what are frameand image?
看到ClassCastException
我假设你在这里做一些非法的事情,几个问题,什么是框架和图像?
If frame is a reference to FrameLayout the you would have to use
如果 frame 是对 FrameLayout 的引用,则您必须使用
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(theBitmap.getWidth(), theBitmap.getHeight());
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(theBitmap.getWidth(), theBitmap.getHeight());
Let me know if that helps.
如果这有帮助,请告诉我。