2

I am trying to create a custom view without xml

public class MyView extends ViewGroup {

    public MyView (Context context) {
        super(context);
        setBackgroundColor(0xffff0000);

        RelativeLayout base = new RelativeLayout(context);

        RelativeLayout.LayoutParams rP = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        base.setBackgroundColor(0xff00ff00);
        base.setLayoutParams(rP);

        RelativeLayout.LayoutParams iP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        ImageView icon = new ImageView(context);
        icon.setBackgroundResource(android.R.drawable.ic_menu_add);
        icon.setLayoutParams(iP);

        addView(icon);
        addView(base);
    }

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

    }
}

And use it in actitvy

MyView myview = new MyView(this);
WindowManager.LayoutParams baseParams = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.MATCH_PARENT,
        WindowManager.LayoutParams.MATCH_PARENT,
        WindowManager.LayoutParams.TYPE_PHONE,
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
        PixelFormat.TRANSLUCENT);
addContentView(myview, baseParams);

But the LayoutParams does not work, it display nothing

I must use this onLayout to make it display, and the base layout is not MATCH_PARENT and icon is not WRAP_CONTENT

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final View child = getChildAt(0);
    final View child2 = getChildAt(1);

    child.layout(0, 0, 300, 300);
    child2.layout(0, 0, 300, 300);
}

I also tried to add icon to layout, but the app will crash

base.addView(icon);

Is it possible to create this layout without hardcode the size and position?

I checked RelativeLayout.LayoutParams, but I cannot find any method to set it centerInParent to true.

http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_menu_add"
        android:layout_centerInParent="true"
        />
</RelativeLayout>
CL So
  • 3,647
  • 10
  • 51
  • 95
  • 2
    `WindowManager.LayoutParams` are not for `View`s within an `Activity`. I'm not sure what you're trying to do, exactly, but if you want your custom `View` as the entire layout, just call `setContentView()` with it. Otherwise, add it to a `ViewGroup` that's in the `Activity`'s layout. For centering with the `RelativeLayout.LayoutParams`, you're looking for the `addRule()` method. – Mike M. May 15 '16 at 16:45
  • Also, `ViewGroup` does not know anything about laying out its children. Either actually implement the rest of the code for a custom `ViewGroup` (such as filling in the empty `onLayout()` method) or extend some other class that has specific layout rules (e.g., extend `RelativeLayout` instead of trying to wrap `RelativeLayout`). – CommonsWare May 15 '16 at 16:54

1 Answers1

3

Please give this code a try.

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Create parent RelativeLayout
        RelativeLayout relParent = new RelativeLayout(this);
        RelativeLayout.LayoutParams relParentParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        relParent.setLayoutParams(relParentParam);

        // Create child ImageView
        ImageView imgView = new ImageView(this);
        RelativeLayout.LayoutParams imgViewParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        imgView.setLayoutParams(imgViewParams);
        imgView.setImageResource(android.R.drawable.ic_menu_add);
        imgViewParams.addRule(RelativeLayout.CENTER_IN_PARENT);

        // Add child ImageView to parent RelativeLayout
        relParent.addView(imgView);

        // Set parent RelativeLayout to your screen
        setContentView(relParent, relParentParam);
    }

}
Harsh4789
  • 677
  • 7
  • 17