我有一个片段,在onCreate方法中,我添加了一个FrameLayout
。对于这个FrameLayout,我添加了一个从View扩展的类的对象。
现在我想将此视图对象的宽度和高度设置为wrap_content
。
这是我的代码和我尝试的内容:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.fragmentTest, container, false);
FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.drawingImg);
DrawingImg drawingImgView = new DrawingImg();
drawingImgView.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
drawingImgView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
// adding the view to the framelayout with the drawn picture
frameLayout.addView(drawingImgView);
return view;
}
fragmentTest.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawingImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</FrameLayout>
DrawingImg类从View扩展并使用canvas
绘制图像public class DrawingImg extends View {
....
}
我的目标是框架布局只占据所需的宽度和高度
答案 0 :(得分:0)
试试这个:
drawingImgView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
答案 1 :(得分:0)
使用:
drawingImgView.setLayoutParams(new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT));
而不是:
drawingImgView.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
drawingImgView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;