我在View类中创建一个不会破坏程序的工作布局时遇到问题。我认为代码中的所有内容都已正确完成,但可能有一些我不知道的设置,我希望你们能向我揭示这一点。
我正在尝试使用“清除”按钮和RadioGroup绘制视图。
首先,所有这一切都运行良好,但两个添加的内容视图(btnReset =“Clear”,btnScale = RadioGroup)相互遮挡。这就是为什么我想添加一个父布局,所以我可以轻松管理这些视图。
File1.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DrawView tv = new DrawView(this);
setContentView(tv);
addContentView(tv.btnReset, tv.params);
addContentView(tv.btnScale, tv.paramsRadio);}
DrawView.java
public class DrawView extends View {
public Button btnReset;
public RelativeLayout.LayoutParams params;
public RelativeLayout.LayoutParams paramsRadio;
public RadioGroup btnScale;
public DrawView(Context context) {
super(context);
btnReset = new Button(context);
btnReset.setText("Clear Screen");
btnScale = new RadioGroup(context);
btnScale.setOrientation(RadioGroup.HORIZONTAL);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
paramsRadio = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
我省略了大部分代码,因为我觉得它无关紧要,一切正常,但布局。
无论如何,当我尝试创建一个布局时,我只需添加一些代码:
更改了File1.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DrawView tv = new DrawView(this);
setContentView(tv);
addContentView(tv.layout, tv.params); }
更改了DrawView.java
public class DrawView extends View {
public Button btnReset;
public RelativeLayout.LayoutParams params;
public RelativeLayout.LayoutParams paramsRadio;
public RelativeLayout layout;
public RadioGroup btnScale;
public DrawView(Context context) {
super(context);
btnReset = new Button(context);
btnReset.setText("Clear Screen");
btnScale = new RadioGroup(context);
btnScale.setOrientation(RadioGroup.HORIZONTAL);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
paramsRadio = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layout.addView(btnReset, params);
layout.addView(btnScale, paramsRadio);
我认为它应该像魅力一样工作,但不幸的是,当我尝试用新创建的布局做任何事情时,我的应用程序崩溃了。该代码有什么问题?
答案 0 :(得分:0)
好吧,经过长时间的战斗,我设法解决了我自己的问题:D我没有这样做,就像@pskink建议的那样,因为老实说对我来说太先进了。我找到了更简单的解决方案。
问题是,你可以在DrawView.java中创建尽可能多的视图,但在File1.java中创建的Layout中对它们进行排序!
就像那样: File1.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
DrawView tv = new DrawView(this);
ll.addView(tv.btnReset, tv.params);
ll.addView(tv.btnScale, tv.paramsRadio);
ll.addView(tv);
setContentView(ll);
DrawView.java保持不变:
public class DrawView extends View {
public Button btnReset;
public LinearLayout.LayoutParams params;
public LinearLayout.LayoutParams paramsRadio;
public RadioGroup btnScale;
public DrawView(Context context) {
super(context);
btnReset = new Button(context);
btnReset.setText("Clear Screen");
btnScale = new RadioGroup(context);
btnScale.setOrientation(RadioGroup.HORIZONTAL);
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
paramsRadio = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);