所以我在代码中设置了一个自定义视图(没有为它定义XML布局),我想知道如何正确定义子视图ID.我DO有定义ID的类似于这样的XML文件.但我所理解的吧,至少,因为我没有一个XML布局没有AttribSet传递..所以我的所有构造函数是刚(上下文的背景下)的类型.
<resources>
<item type="id" name="textview1"/>
<item type="id" name="textview2"/>
</resources>
Run Code Online (Sandbox Code Playgroud)
我的观点看起来像这样:
public class MyView extends View {
protected RelativeLayout baseLayout;
protected TextView textView1;
protected TextView textView2;
public MyView(Context context) {
super(context);
LayoutParams fill = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
setLayoutParams(fill);
Resources res = getResources();
baseLayout = new RelativeLayout(context);
baseLayout.setLayoutParams(fill);
textView1 = new TextView(context);
textView1.setId(R.id.textview1);
// other init stuff here
textView2 = new TextView(context);
textView2.setId(R.id.textview2);
// other init stuff here
baseLayout.addView(textView1);
baseLayout.addView(textView2);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
// *snip lots of stuff*
baseLayout.draw(canvas);
}
// This is only here because findViewById() returns NULL when I search
public TextView getTextView1() {
return textView1;
}
// This is only here because findViewById() returns NULL when I search
public TextView getTextView2() {
return textView2;
}
}
Run Code Online (Sandbox Code Playgroud)
这是使用视图的Activity.
public class MyActivity extends Activity {
// This is only here because findViewById() returns NULL when I search
private MyView myView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Resources res = getResources();
myView = new myView(this);
setContentView(myView);
// This returns NULL. :/
// final TextView textView1 = (TextView) findViewById(R.id.textView1);
// This is only here because findViewById() returns NULL when I search..
final TextView textView1 = myView.getTextView1();
final Animation anim = AnimationUtils.loadAnimation(this, R.anim.my_animation);
textView1.setAnimation(anim);
anim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation _anim) {
Log.v("InsideAnimation", "Animation ended");
textView1.setVisibility(View.INVISIBLE);
}
@Override
public void onAnimationRepeat(Animation _anim) {
Log.v("InsideAnimation", "Animation repeated");
}
@Override
public void onAnimationStart(Animation _anim) {
Log.v("InsideAnimation", "Animation started");
}
});
anim.reset();
textView1.startAnimation(anim);
}
}
Run Code Online (Sandbox Code Playgroud)
主要的问题是我加载的动画(在xml中定义)和启动绝对没有.我得到一个"动画开始"的日志声明,但就是这样.它永远不会结束,当我模仿它时它永远不会做任何事情.它就像它开始,但实际上并没有运行.我认为问题是动画可能希望使用xml id布局定义来完成它,但由于findViewById()返回NULL,我假设该部分被破坏(因此动画不起作用).所以...至少现在,我想知道我在设置ID时遇到了什么问题,这样我就可以让findViewById()返回正确的视图而不是NULL ...然后动画可能会起作用.(或者如果在那之后它仍然不起作用,我将不得不考虑在代码中定义动画而不是xml)
任何帮助或建议表示赞赏.
我的猜测是,您的 TextView 此时尚未实例化。尝试在 oncreate-method 之外触发动画。
\n\n此外,您的 MyView 是从 View 扩展的,并且您\xc2\xb4正在内部创建一个relativelayout。如果你这样做,你也可以直接从RelativeLayout扩展你的类,然后做类似的事情
\n\nmRelativeLayout = new MyRelativeLayout(context)\nmRelativeLayout.findViewById(R.id.textview1)\nRun Code Online (Sandbox Code Playgroud)\n\n也可能是问题,因为你还没有\xc2\xb4t在你的活动中获得对RelativeLayout的引用。虽然我的第一个猜测更有可能是解决方案......
\n\n哦,您可以简单地在 xml 中设计您的relativelayout。我认为在这里以编程方式使用布局没有真正的意义......
\n| 归档时间: |
|
| 查看次数: |
4946 次 |
| 最近记录: |