Android 仍然无法获取片段内部的图像大小
我试过了,还有,还有其他的。但它总是返回0。还是我错过了什么 这是我的代码:Android 仍然无法获取片段内部的图像大小,android,android-fragments,Android,Android Fragments,我试过了,还有,还有其他的。但它总是返回0。还是我错过了什么 这是我的代码: @SuppressLint("NewApi") public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_photo_editor, container,
@SuppressLint("NewApi")
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_photo_editor, container, false);
img = (ImageView) rootView.findViewById(R.id.photo_editor_img);
watermark = (ImageView) rootView.findViewById(R.id.photo_editor_watermark);
WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
if (android.os.Build.VERSION.SDK_INT >= 13){
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
}
else screenWidth = display.getWidth();
ViewTreeObserver vto = watermark.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
watermarkWidth = watermark.getWidth();
watermarkHeight = watermark.getHeight();
}
});
vto = img.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
scale = (float) (img.getWidth()/screenWidth);
}
});
return rootView;
}
我尝试替换onResume和onActivityCreated中的代码,但仍然返回0
这是我的xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/photo_editor_root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true" >
<ImageView android:id="@+id/photo_editor_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/bg" />
<ImageView android:id="@+id/photo_editor_watermark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|left"
android:layout_margin="5dp"
android:src="@drawable/watermark"/>
</FrameLayout>
</RelativeLayout>
试试这个:如果您正在为imageView使用wrap_内容,并且没有设置任何图像,那么它将返回零大小。如果没有,请尝试在xml中设置任何图像。顺便说一句,您缺少super.onCreateViewinflater、container、savedInstanceState;并不是说它会修复一些东西…@vakman我已经在ImageView中设置了一个图像,我已经添加了我的xml。@helleye我从来没有在每个onCreateView中使用过该代码行,也从来没有出现过错误,但我真的不知道它的用途。。