是否可以在Android中创建没有xml的视图?

是否可以在Android中创建没有xml的视图?,android,android-layout,android-view,android-viewgroup,Android,Android Layout,Android View,Android Viewgroup,我正在尝试创建一个没有xml的自定义视图 public class MyView extends ViewGroup { public MyView (Context context) { super(context); setBackgroundColor(0xffff0000); RelativeLayout base = new RelativeLayout(context); RelativeLayout.La

我正在尝试创建一个没有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) {

    }
}
并在活动中使用它

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);
但是LayoutParams不起作用,它不显示任何内容

我必须使用此onLayout使其显示,并且基本布局不匹配\u父级,图标不包含内容

@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);
}
我还试图在布局中添加图标,但应用程序将崩溃

base.addView(icon);
是否可以在不硬编码尺寸和位置的情况下创建此布局

我检查了RelativeLayout.LayoutParams,但找不到任何方法将其设置为true


请尝试一下此代码

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);
    }

}

WindowManager.LayoutParams
不用于
活动中的
视图
s。我不确定您到底想做什么,但是如果您希望自定义
视图作为整个布局,只需调用
setContentView()
。否则,将其添加到
活动
布局中的
视图组
。对于使用
RelativeLayout.LayoutParams
居中,您需要查找
addRule()
方法。此外,
ViewGroup
不知道如何布置其子对象。实际实现自定义
视图组
的其余代码(例如填充空的
onLayout()
方法)或扩展其他具有特定布局规则的类(例如,扩展
RelativeLayout
而不是尝试包装
RelativeLayout
)。可以,但title要求创建不带XML的视图
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);
    }

}