通过使用扩展视图类的setContentView [英] setContentView by using extending view class
本文介绍了通过使用扩展视图类的setContentView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的扩展视图设置一个activity..what的内容正在发生的事情是显示设置文本的空白activity.instead。
公共类ThreadCheck延伸活动{
MyView的观点;
/ *(非Javadoc中)
* @see android.app.Activity#的onCreate(android.os.Bundle)
* /
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
上下文CTX = getApplicationContext();
鉴于=新MyView的(CTX);
的setContentView(视图);
}
}
公共类MyView的扩展视图{
TextView的tvThread;
公共MyView的(上下文的背景下){
超级(上下文);
tvThread =新的TextView(背景);
tvThread.setText(你好,这是MyView的);
// TODO自动生成构造函数存根
} }
解决方案
更改此
查看=新的MyView的(CTX);
到
查看=新的MyView的(ThreadCheck.this);
和变更
公共类MyView的扩展TextView的{ 公共MyView的(上下文的背景下){
超级(上下文);
this.setText(你好,这是MyView的);
}}
编辑:在评论的问题。你需要一个ViewGroup中向其中添加你的看法。
公共类MyView的扩展RelativeLayout的{ 公共MyView的(上下文的背景下){
超级(上下文);
RelativeLayout.LayoutParams的LayoutParams =新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
this.setLayoutParams(的LayoutParams); RelativeLayout.LayoutParams params1 =新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); TextView的电视=新的TextView(背景);
tv.setText(你好);
tv.setId(1); 按钮B =新按钮(背景);
params1.addRule(RelativeLayout.BELOW,tv.getId());
b.setText(你好);
b.setId(2); this.addView(电视);
this.addView(B,params1); }
}
I am using extends View for setting content of an activity..what is happening it is showing blank activity.instead of setting text.
public class ThreadCheck extends Activity{
MyView view;
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Context ctx = getApplicationContext();
view=new MyView(ctx);
setContentView(view);
}
}
public class MyView extends View{
TextView tvThread;
public MyView(Context context) {
super(context);
tvThread=new TextView(context);
tvThread.setText("Hello This Is Myview");
// TODO Auto-generated constructor stub
}
}
解决方案
Change this
view=new MyView(ctx);
to
view=new MyView(ThreadCheck.this);
And change to
public class MyView extends TextView{
public MyView(Context context) {
super(context);
this.setText("Hello This Is Myview");
}
}
Edit: to the question in comment. You need a ViewGroup to which you add your views.
public class MyView extends RelativeLayout{
public MyView(Context context) {
super(context);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
this.setLayoutParams(layoutParams);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(context);
tv.setText("hello");
tv.setId(1);
Button b = new Button(context);
params1.addRule(RelativeLayout.BELOW, tv.getId());
b.setText("Hi");
b.setId(2);
this.addView(tv);
this.addView(b, params1);
}
}
这篇关于通过使用扩展视图类的setContentView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文