Java Can';t将视图添加到自定义RelativeLayout

Java Can';t将视图添加到自定义RelativeLayout,java,android,android-layout,android-studio,android-relativelayout,Java,Android,Android Layout,Android Studio,Android Relativelayout,我对Android编程(最初是iOS开发者)非常陌生,对于以编程方式添加视图我有点困惑。我有一个自定义的相对布局,它在我的活动中显示得很好,我必须动态地向其中添加视图。这就是我被困的地方。。。下面是我的自定义视图: public class BoardView extends RelativeLayout { // Initialisation @Override public void onMeasure(int widthMeasureSpec, int heightMea

我对Android编程(最初是iOS开发者)非常陌生,对于以编程方式添加视图我有点困惑。我有一个自定义的相对布局,它在我的活动中显示得很好,我必须动态地向其中添加视图。这就是我被困的地方。。。下面是我的自定义视图:

public class BoardView extends RelativeLayout {

    // Initialisation
    @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int size = width > height ? height : width;
        setMeasuredDimension(size, size);
        this.addView(new Button(this.getContext()));
    }

    // Required constructors
    public BoardView(Context context) {
        super(context);
    }
    public BoardView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public BoardView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
}
然后在我的活动中,我尝试这样做:

public class MainGameView extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game_activity);


        RelativeLayout layout = (RelativeLayout) findViewById(R.id.view);
        Button bouton = new Button(this);
        bouton.setText("ok");
        bouton.setTextColor(Color.BLACK);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(WRAP_CONTENT,WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
        params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
        params.leftMargin = 107;
        layout.addView(bouton, params);
    }
}
对我来说似乎很简单,但无法实现这一点。。。BoardView仍为空。谁能解释一下我该怎么做?先谢谢你

如果您需要xml来帮助我,这里是: (最底部的BoardView)


编辑: 应用程序的屏幕截图。灰色的正方形是董事会视图。我希望在里面画一个按钮,但它没有。。。

您确定它不仅被另一个元素隐藏了吗?你的相对速度只有100dp高,里面还有很多其他物体。你能展示一个屏幕截图吗?100dp高相对视图是另一个,我说的是xml文件底部的“BoardView”。我将用截图编辑我的帖子。编辑:无法放置图片,因为我是新来的。所以链接指向屏幕截图;)我假设这是一个调试试用,但不要在
onMeasure()方法中添加
View
s。也就是说,删除
this.addView(…)行。此外,如果您没有在
onMeasure()
中调用
super
方法,则需要通过适当地调用其
measure()
方法,自行测量每个子视图。否则,子
视图
s的宽度和高度都将为0。编辑了我的代码,添加super.onMeasure解决了这个问题。不知道为什么我忘了,非常感谢!您确定它不仅被另一个元素隐藏吗?你的相对速度只有100dp高,里面还有很多其他物体。你能展示一个屏幕截图吗?100dp高相对视图是另一个,我说的是xml文件底部的“BoardView”。我将用截图编辑我的帖子。编辑:无法放置图片,因为我是新来的。所以链接指向屏幕截图;)我假设这是一个调试试用,但不要在
onMeasure()方法中添加
View
s。也就是说,删除
this.addView(…)行。此外,如果您没有在
onMeasure()
中调用
super
方法,则需要通过适当地调用其
measure()
方法,自行测量每个子视图。否则,子
视图
s的宽度和高度都将为0。编辑了我的代码,添加super.onMeasure解决了这个问题。不知道为什么我忘了,非常感谢!
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:background="#ffffff">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/barre_menu"
        android:id="@+id/relativeLayout">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="28dp"
            android:text="SCORE"
            android:id="@+id/textViewScore"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:textColor="#ffffff"
            android:layout_marginBottom="20dp"
            android:textSize="24sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SCORE"
            android:id="@+id/textView3"
            android:layout_centerHorizontal="true"
            android:layout_gravity="bottom"
            android:layout_alignParentEnd="false"
            android:layout_above="@+id/textViewScore"
            android:textColor="#ffffff"
            android:textSize="13sp" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="28dp"
            android:layout_height="wrap_content"
            android:text=""
            android:id="@+id/button"
            android:layout_alignTop="@+id/textViewScore"
            android:layout_alignBottom="@+id/textViewScore"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="20dp"
            android:background="@drawable/picto_menu" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="28dp"
            android:layout_height="wrap_content"
            android:text=""
            android:id="@+id/button2"
            android:background="@drawable/picto_restart"
            android:layout_alignBottom="@+id/textViewScore"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginRight="20dp"
            android:layout_alignTop="@+id/textViewScore" />
    </RelativeLayout>

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true">
    </com.google.android.gms.ads.AdView>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_below="@+id/relativeLayout"
        android:layout_centerHorizontal="true"
        android:id="@+id/frameLayout">

        <fr.wiboyd.arnaud.kilo.Views.TileButton
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="0"
            android:id="@+id/button3"
            android:layout_gravity="center" />
    </FrameLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/frameLayout"
        android:layout_centerHorizontal="true"
        android:layout_above="@+id/adView">

        <fr.wiboyd.arnaud.kilo.Views.BoardView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/view"
            android:layout_gravity="center"
            android:background="#cccccc" />

    </FrameLayout>
</RelativeLayout>