How to place a custom view inside a custom view Group / Layout

the custom view inside the custom view of the GroupGroup is not displayed, how can I display it? or is there a better way to make this work?

there is no compilation or runtime errors, but the view does not appear in the viewGroup, it should fill the area with color, like other views, but it is white and the color for the view does not appear inside CustomLayout

xml, the first 2 views are displayed without problems, but the 3rd view, which is nested inside CustomLayout, is not displayed, just a white area, the view inside is invisible

CustomViewOne is a separate class file, CustomViewTwo and CustomViewThree are inserted inside the MainActivity class as static inner classes, and CustomLayout is a separate file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<com.example.customviewexample.CustomViewOne
    android:layout_width="100dp"
    android:layout_height="50dp" />

<view 
    class="com.example.customviewexample.MainActivity$CustomViewTwo"
    android:layout_width="100dp"
    android:layout_height="50dp" />

<com.example.customviewexample.CustomLayout
    android:layout_width="100dp"
    android:layout_height="50dp">

    <view 
        class="com.example.customviewexample.MainActivity$CustomViewThree"
           android:layout_width="match_parent"
           android:layout_height="match_parent" />

</com.example.customviewexample.CustomLayout>

</LinearLayout>

here is the code for CustomViewThree, easily changing, like other custom views that it just fills the area with color, it is nested inside MainActivity, so you need to use MainActivity $ CustomViewThree to access it.

public static class CustomViewThree extends View {

public CustomViewThree(Context context) {
    super(context);

}

public CustomViewThree(Context context, AttributeSet attrs) {
    super(context, attrs);

}

public CustomViewThree(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

}

@Override
protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);

    canvas.drawColor(Color.GREEN);
}

}

and here is the code for the CustomLayout class

public class CustomLayout extends FrameLayout {

public CustomLayout(Context context) {
    super(context);
   init(context);
}

public CustomLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
   init(context);
}

public CustomLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
   init(context);
}

public void init(Context context) {

}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {

}

}
+4
source share
1 answer

the custom view inside the custom group view is not displayed, how can I get it to show?

CustomLayout, , onLayout(), . ViewGroup, . , ( layout() ). CustomLayout extends FrameLayout super FrameLayout ( ?).

+4

Source: https://habr.com/ru/post/1528961/


All Articles