Я создал простую группу представлений (LcarsLayout), расширенную от AbsoluteLayout (я знаю, что она устарела).
В инспекторе макетов я добавил дочернее представление в LcarsLayout, я могу выбрать дочернее представление в представлениисписок дерева и редактировать свойства, но я не могу выбрать это дочернее представление на скриншоте само.
Я не вижу, что мне здесь не хватает.
package nl.spectronix.basestation.lcars;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.Display;
import android.view.View;
import android.view.ViewDebug;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.RemoteViews;
import androidx.annotation.IdRes;
import androidx.annotation.InspectableProperty;
import java.util.HashMap;
import nl.spectronix.basestation.R;
@RemoteViews.RemoteView
public class LcarsLayout extends AbsoluteLayout implements LcarsInterface {
int deviceWidth;
SparseArray<View> mChildrenByIds = new SparseArray<>();
// private boolean mDirtyHierarchy = true;
private HashMap<String, Integer> mDesignIds = new HashMap<>();
/**
* @hide
*/
public final static int DESIGN_INFO_ID = 0;
private LCARS.LcarsSchema lcarsColorSchema;
/**
* @hide
*/
public void setDesignInformation(int type, Object value1, Object value2) {
if (type == DESIGN_INFO_ID && value1 instanceof String && value2 instanceof Integer) {
if (mDesignIds == null) {
mDesignIds = new HashMap<>();
}
String name = (String) value1;
int index = name.indexOf("/");
if (index != -1) {
name = name.substring(index + 1);
}
int id = (Integer) value2;
mDesignIds.put(name, id);
}
}
/**
* @hide
*/
public Object getDesignInformation(int type, Object value) {
if (type == DESIGN_INFO_ID && value instanceof String) {
String name = (String) value;
if (mDesignIds != null && mDesignIds.containsKey(name)) {
return mDesignIds.get(name);
}
}
return null;
}
public LcarsLayout(Context context) {
this(context, null, 0);
}
public LcarsLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public LcarsLayout(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public LcarsLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initFromAttributes(context, attrs, defStyleAttr, defStyleRes);
}
private void initFromAttributes(
Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.LcarsLayout, defStyleAttr, defStyleRes);
try {
int x = a.getInt(R.styleable.LcarsLayout_LcarsShema, -1);
lcarsColorSchema = (x == -1)? null: LCARS.LcarsSchema.values()[x];
} finally {
a.recycle();
}
final Display display = ((WindowManager)
context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Point deviceDisplay = new Point();
display.getSize(deviceDisplay);
deviceWidth = deviceDisplay.x;
}
@Override
public void setId(int id) {
mChildrenByIds.remove(getId());
super.setId(id);
mChildrenByIds.put(getId(), this);
}
/**
* {@inheritDoc}
*/
@Override
public void onViewAdded(View view) {
super.onViewAdded(view);
mChildrenByIds.put(view.getId(), view);
}
/**
* {@inheritDoc}
*/
@Override
public void onViewRemoved(View view) {
super.onViewRemoved(view);
mChildrenByIds.remove(view.getId());
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount();
int curWidth, curHeight, curLeft, curTop, maxHeight;
//get the available size of child view
final int childLeft = this.getPaddingLeft();
final int childTop = this.getPaddingTop();
final int childRight = this.getMeasuredWidth() - this.getPaddingRight();
final int childBottom = this.getMeasuredHeight() - this.getPaddingBottom();
final int childWidth = childRight - childLeft;
final int childHeight = childBottom - childTop;
maxHeight = 0;
curLeft = childLeft;
curTop = childTop;
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
if (child.getVisibility() == GONE)
return;
LcarsLayout.LayoutParams params = (LcarsLayout.LayoutParams) child.getLayoutParams();
curWidth = child.getMeasuredWidth();
curHeight = child.getMeasuredHeight();
curLeft = childLeft + params.x;
curTop = childTop + params.y;
//do the layout
child.layout(curLeft, curTop, curLeft + curWidth, curTop + curHeight);
//store the max height
}
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LcarsLayout.LayoutParams(getContext(), attrs);
}
/**
* Returns a set of layout parameters with a width of
* {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT},
* a height of {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT} and no spanning.
*/
@Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(200, 20);
}
// Override to allow type-checking of LayoutParams.
@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return p instanceof LcarsLayout.LayoutParams;
}
@Override
protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
return new LayoutParams(lp);
}
@Override
public CharSequence getAccessibilityClassName() {
return LcarsLayout.class.getName();
}
@ViewDebug.ExportedProperty(category = "lcars")
@InspectableProperty(name = "LcarsSchema")
public LCARS.LcarsSchema getLcarsSchema() {
return lcarsColorSchema;
}
@RemotableViewMethod
public void setLcarsSchema(LCARS.LcarsSchema schema) {
System.out.println(this + " setLcarsSchema()");
lcarsColorSchema = schema;
invalidate();
requestLayout();
}
@Override
public boolean shouldDelayChildPressedState() {
return false;
}
public static class LayoutParams extends AbsoluteLayout.LayoutParams {
/**
* References the id of the parent.
*/
public static final int PARENT_ID = 0;
/**
* Defines an id that is not set.
*/
public static final int UNSET = -1;
private int LeftConnection = UNSET;
private int RightConnection = UNSET;
private int TopConnection = UNSET;
private int BottomConnection = UNSET;
public boolean autoHeight = true;
public boolean autoWidth = true;
public LayoutParams(int width, int height) {
super(width, height,0,0);
}
public LayoutParams(int width, int height,int x, int y) {
super(width, height,x,y);
}
public LayoutParams(ViewGroup.LayoutParams source) {
super(source);
}
public LayoutParams(LayoutParams source) {
super(source);
this.LeftConnection = source.LeftConnection;
this.RightConnection = source.RightConnection;
this.TopConnection = source.TopConnection;
this.BottomConnection = source.BottomConnection;
this.autoHeight = source.autoHeight;
this.autoWidth = source.autoWidth;
// this.rightToRight = source.rightToRight;
}
public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.LcarsLayout_Layout);
final int N = a.getIndexCount();
for (int i = 0; i < N; i++) {
int attr = a.getIndex(i);
if (attr == R.styleable.LcarsLayout_Layout_LcarsConnectLeft) {
LeftConnection = (a.getResourceId(attr, getLeftConnection()));
if (LeftConnection == UNSET) {
LeftConnection = (a.getInt(attr, UNSET));
}
} else if (attr == R.styleable.LcarsLayout_Layout_LcarsConnectRight) {
RightConnection =(a.getResourceId(attr, getRightConnection()));
if (RightConnection == UNSET) {
RightConnection =(a.getInt(attr, UNSET));
}
} else if (attr == R.styleable.LcarsLayout_Layout_LcarsConnectTop) {
TopConnection = (a.getResourceId(attr, getTopConnection()));
if (TopConnection == UNSET) {
TopConnection = (a.getInt(attr, UNSET));
}
} else if (attr == R.styleable.LcarsLayout_Layout_LcarsConnectBottom) {
BottomConnection = (a.getResourceId(attr, getBottomConnection()));
if (BottomConnection == UNSET) {
BottomConnection = (a.getInt(attr, UNSET));
}
} else if (attr == R.styleable.LcarsLayout_Layout_LcarsAutoHeight) {
autoHeight = a.getBoolean(attr,true);
} else if (attr == R.styleable.LcarsLayout_Layout_LcarsAutoWidth) {
autoWidth = a.getBoolean(attr,true);
}
}
a.recycle();
}
@IdRes
@ViewDebug.ExportedProperty(category = "lcars")
@InspectableProperty(name = "LcarsConnectLeft")
public int getLeftConnection() {
return LeftConnection;
}
@IdRes
@ViewDebug.ExportedProperty(category = "lcars")
@InspectableProperty(name = "LcarsConnectRight")
public int getRightConnection() {
return RightConnection;
}
@IdRes
@ViewDebug.ExportedProperty(category = "lcars")
@InspectableProperty(name = "LcarsConnectTop")
public int getTopConnection() {
return TopConnection;
}
@IdRes
@ViewDebug.ExportedProperty(category = "lcars")
@InspectableProperty(name = "LcarsConnectBottom")
public int getBottomConnection() {
return BottomConnection;
}
@RemotableViewMethod
public void setLeftConnection(int leftConnection) {
System.out.println(this + " setLcarsSchema()");
LeftConnection = leftConnection;
}
}
}