我正在尝试将在我的自定义视图组下定义的视图移动到另一个RelativeLayout中,但是当我到达将视图添加到新视图组的代码时,它会崩溃。
我的自定义视图组扩展了RelativeLayout,并使此布局膨胀:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/view_image_with_bottom_overlay_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"/>
<RelativeLayout
android:id="@+id/view_image_with_bottom_overlay_overlay_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
android:background="@drawable/bg_gradient_bottom_up">
</RelativeLayout>
</RelativeLayout>
ImageWithBottomOverlayView:
public class ImageWithBottomOverlayView extends FrameLayout {
public static final int NON_EXISTING_RESOURCE = -1;
private ImageView imageView;
private RelativeLayout statusContainer;
public ImageWithBottomOverlayView(Context context) {
super(context);
this.init(context);
}
public ImageWithBottomOverlayView(Context context, AttributeSet attrs) {
super(context, attrs);
this.init(context);
this.processAttrs(context, attrs);
}
public ImageWithBottomOverlayView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.init(context);
this.processAttrs(context, attrs);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public ImageWithBottomOverlayView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
this.init(context);
this.processAttrs(context, attrs);
}
private void init(Context context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rootview = inflater.inflate(R.layout.view_image_with_bottom_overlay, this, true);
this.imageView = (ImageView) rootview.findViewById(R.id.view_image_with_bottom_overlay_image);
this.statusContainer =
(RelativeLayout) rootview.findViewById(R.id.view_image_with_bottom_overlay_overlay_container);
this.moveChildrenToStatusContainer()
}
private void moveChildrenToStatusContainer() {
int childCount = this.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = this.getChildAt(i);
this.removeView(child);
ViewGroup.LayoutParams layoutParams = child.getLayoutParams();
RelativeLayout.LayoutParams newLayoutParams = new RelativeLayout.LayoutParams(layoutParams);
child.setLayoutParams(newLayoutParams);
this.statusContainer.addView(child);// it fails when this is called
}
}
private void processAttrs(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ImageWithBottomOverlayView, 0, 0);
int resourceId = a.getResourceId(R.styleable.ImageWithBottomOverlayView_imageSource, NON_EXISTING_RESOURCE);
boolean isOverlayAlignStart = a.getBoolean(R.styleable.ImageWithBottomOverlayView_overlay_align_start, true);
a.recycle();
setImage(resourceId);
setBottomOverlayGravity(isOverlayAlignStart);
}
private void setBottomOverlayGravity(Boolean isOverlayAlignLeft) {
int gravity = this.statusContainer.getGravity();
if (isOverlayAlignLeft) {
gravity |= Gravity.START;
} else {
gravity |= Gravity.END;
}
this.statusContainer.setGravity(gravity);
}
public void setImage(int resId) {
if (resId != NON_EXISTING_RESOURCE) {
imageView.setImageResource(resId);
}
}
public ImageView getImageView() {
return this.imageView;
}
}
状态容器是内部RelativeLayout view_image_with_bottom_overlay_overlay_container
init()
和moveChildrenToStatusContainer()
是ImageWithBottomOverlayView的主要部分,init在所有构造函数中都被调用。
当我到达addView()
部分时,它会崩溃
android.view.InflateException: Binary XML file line #9: Error inflating class com.acme.ImageWithBottomOverlayView
以下是我的预期用途:
<com.acme.ImageWithBottomOverlayView
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/task_scan_attach_image_location_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_location_on_white_18dp"/>
</com.acme.ImageWithBottomOverlayView>