如何以编程方式将视图添加到“我的活动”主布局中的线性布局。到目前为止,我已经尝试了下面的代码。我是一个初学者,所以我不知道我的代码出了什么问题。java是一个用于绘图的类。这是一个自定义视图类。
//MainActivity.java
package com.shahroze.myworld;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
LinearLayout controlpanel;
LinearLayout simulation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
controlpanel = (LinearLayout) findViewById(R.id.controlLayout);
simulation = (LinearLayout) findViewById(R.id.simulationLayout);
Drawresult();
}
protected void Drawresult() {
setContentView(R.layout.activity_main);
//how to add views to simulation and control panel
simulation.addView(new pixelGrid(this));//this line makes the app crash.
}
暂无答案!
目前还没有任何答案,快来回答吧!