LayoutParams of custom ViewGroup not showing in android studio editor

I have a class that extends LinearLayout

- it is almost identical, it is just slightly different.

public class CustomLinearLayout extends LinearLayout {
    <Standard constructors...>

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        <Custom measuring code here>
    }
}

      

This works great, but now none of the children show ANY LinearLayout LayoutParams attributes in the properties of the layout editor in Android Studio, even something like layout:width

.

My guess is that LayoutParams are not inherited, so I just duplicated LinearLayout.LayoutParams

and added it everywhere, that is:

public class CustomLinearLayout extends LinearLayout {
    public static class LayoutParams extends LinearLayout.LayoutParams {

        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
        }

        public LayoutParams(int width, int height) {
            super(width, height);
        }

        public LayoutParams(int width, int height, float weight) {
            super(width, height, weight);
        }

        public LayoutParams(ViewGroup.LayoutParams p) {
            super(p);
        }

        public LayoutParams(MarginLayoutParams source) {
            super(source);
        }

        public LayoutParams(LinearLayout.LayoutParams source) {
            super(source);
        }
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new LayoutParams(getContext(), attrs);
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }

    @Override
    protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
        return new LayoutParams(p);
    }

    @Override
    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
        return p instanceof LayoutParams;
    }

      

And, in attrs.xml:

<declare-styleable name="com.domain.package.CustomLinearLayout_Layout">
    <attr name="layout_width" format="dimension">
        <enum name="fill_parent" value="-1" />
        <enum name="match_parent" value="-1" />
        <enum name="wrap_content" value="-2" />
    </attr>
</declare-styleable>

      

Here is the layout view:

<?xml version="1.0" encoding="utf-8"?>

<com.domain.package.CustomLinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

      

Basically as a test to see if it works. However, nothing appears in the editor. I compiled the project and tried it declare-styleable

with and without the full package name. What am I missing?

(EDIT: just to clarify the end goal - I want the attributes to LinearLayout.LayoutParams

be visible in the Android Studio editor). I was hoping they were inherited automatically, but since they are not, I'm trying to duplicate them by subclassing LinearLayout.LayoutParams

, but that won't work either. If there is a way to just inherit LinearLayout

styleables, that would be preferable.)

+3


source to share





All Articles