I have a fragment and in its onCreate method, I add a FrameLayout
. To this FrameLayout I add an object of a class which extends from View.
Now I want to set the width and height of this view object to wrap_content
.
This is my code and what I've tried:
@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>
The DrawingImg class extends from View and draws an image with canvas
public class DrawingImg extends View {
....
}
My aim is that the frame layout occupies only the width and height it needs
尝试这个:
drawingImgView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Use:
drawingImgView.setLayoutParams(new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT));
Instead of:
drawingImgView.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
drawingImgView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.