Android 实现自定义视图和滚动视图?
我不明白的是:Android 实现自定义视图和滚动视图?,android,scrollview,android-custom-view,Android,Scrollview,Android Custom View,我不明白的是: HorizontalScrollView scroll = new HorizontalScrollView(this); scroll.setLayoutParams(new LayoutParams(getResources().getInteger(R.integer.dim_hand_width), getResources().getInteger(R.integer.dim_hand_height))); HandLayout
HorizontalScrollView scroll = new HorizontalScrollView(this);
scroll.setLayoutParams(new LayoutParams(getResources().getInteger(R.integer.dim_hand_width), getResources().getInteger(R.integer.dim_hand_height)));
HandLayout = new LinearLayout(this);
HandLayout.setOrientation(LinearLayout.HORIZONTAL);
HandLayout.setBackgroundColor(getResources().getColor(R.color.bg_hand));
HandLayout.setLayoutParams(new LayoutParams(getResources().getInteger(R.integer.dim_hand_width), getResources().getInteger(R.integer.dim_hand_height)));
scroll.addView(HandLayout);
for (int i = 0; i < 8; i++){
Button b = new Button(this);
b.setWidth(200);
HandLayout.addView(b);
}
HorizontalScrollView滚动=新的HorizontalScrollView(此);
scroll.setLayoutParams(新的LayoutParams(getResources().getInteger(R.integer.dim\u hand\u width),getResources().getInteger(R.integer.dim\u hand\u height));
手动布局=新的线性布局(本);
手动布局。设置方向(线性布局。水平);
setBackgroundColor(getResources().getColor(R.color.bg_hand));
HandLayout.setLayoutParams(新的LayoutParams(getResources().getInteger(R.integer.dim\u hand\u width),getResources().getInteger(R.integer.dim\u hand\u height));
滚动。添加视图(手动布局);
对于(int i=0;i<8;i++){
按钮b=新按钮(此按钮);
b、 设置宽度(200);
HandLayout.addView(b);
}
这显示了带有全部8个按钮的滚动视图,并且它是可滚动的。但是,当我将最后几行替换为:
for (int i = 0; i < 8; i++){
HeroCard hc = new HeroCard(this);
HandLayout.addView(hc);
}
for(int i=0;i<8;i++){
HeroCard hc=新HeroCard(本);
HandLayout.addView(hc);
}
然后什么也看不出来。HeroCard是一个自定义视图,它只实现onDraw和onCreate(上下文)方法(我不会复制代码,因为它就是这样)
我猜Button实现了一个我没有实现的功能,这就是为什么它没有画图
有什么想法吗
多谢各位 您似乎没有为您的
HeroCard
视图设置任何参数,它没有大小,请尝试使用getLayoutParams()
恢复参数并修改宽度使用以下方法:
hc.setWidth(200);
hc.setHeight(50);
在:
for(int i=0;i<8;i++){
HeroCard hc=新HeroCard(本);
HandLayout.addView(hc);
}
并检查输出 你应该为你的HeroCard添加代码,看看你的问题所在。你说的与第二个答案相同,但从技术上讲,另一个答案是正确的,因为它指出了问题所在。无论如何,非常感谢你!
for (int i = 0; i < 8; i++){
HeroCard hc = new HeroCard(this);
HandLayout.addView(hc);
}