【问题标题】:Put TextView inside the View将 TextView 放在 View 中
【发布时间】:2012-06-04 03:25:49
【问题描述】:

我正在尝试这个 SnowFall 项目。 http://code.google.com/p/android-30days-apps/source/browse/trunk/08day/src/com/bakhtiyor/android/snowfall/SnowFall.java?r=27

我需要把TextView放在View类里面(类SnowFallView扩展View,第42行),我该怎么做。

我可以实例化 TextView 并像这样添加它吗?

 package in.isuru.animate;



 public class SnowFall extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            SnowFallView snowFallView = new SnowFallView(this);
            setContentView(snowFallView);
            snowFallView.setBackgroundDrawable(getResources().getDrawable(R.drawable.background_image));

    }

    private class SnowFallView extends View {
            private int snow_flake_count = 5;
            private final List<Drawable> drawables = new ArrayList<Drawable>();
            private int[][] coords;
            private final Drawable snow_flake;
            private TextView countDownView;

            public SnowFallView(Context context) {
                    super(context);
                    setFocusable(true);
                    setFocusableInTouchMode(true);

                    snow_flake = context.getResources().getDrawable(R.drawable.snow_flake);
                    snow_flake.setBounds(0, 0, snow_flake.getIntrinsicWidth(), snow_flake
                            .getIntrinsicHeight());

                    countDownView = new TextView(context);
                    countDownView.setText("It's working");
                    addContentView(countDownView, null);

                    //LayoutInflater inflater = getLayoutInflater();
                    //getWindow().addContentView(inflater.inflate(R.layout.text_layout, null), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                    //ViewGroup.LayoutParams.FILL_PARENT));
            }

            @Override
            protected void onSizeChanged(int width, int height, int oldw, int oldh) {
                    super.onSizeChanged(width, height, oldw, oldh);
                    Random random = new Random();
                    Interpolator interpolator = new LinearInterpolator();

                    snow_flake_count = Math.max(width, height) / 20;
                    coords = new int[snow_flake_count][];
                    drawables.clear();
                    for (int i = 0; i < snow_flake_count; i++) {
                            Animation animation = new TranslateAnimation(0, height / 10
                                    - random.nextInt(height / 5), 0, height + 30);
                            animation.setDuration(10 * height + random.nextInt(5 * height));
                            animation.setRepeatCount(-1);
                            animation.initialize(10, 10, 10, 10);
                            animation.setInterpolator(interpolator);

                            coords[i] = new int[] { random.nextInt(width - 30), -30 };

                            drawables.add(new AnimateDrawable(snow_flake, animation));
                            animation.setStartOffset(random.nextInt(20 * height));
                            animation.startNow();
                    }
            }

            @Override
            protected void onDraw(Canvas canvas) {
                    for (int i = 0; i < snow_flake_count; i++) {
                            Drawable drawable = drawables.get(i);
                            canvas.save();
                            canvas.translate(coords[i][0], coords[i][1]);
                            drawable.draw(canvas);
                            canvas.restore();
                    }
                    invalidate();
            }

    } 
}

但是当我这样做时,我得到了一个错误。

06-04 00:22:22.364:E/AndroidRuntime(359):java.lang.RuntimeException:无法启动活动 ComponentInfo{in.isuru.animate/in.isuru.animate.SnowFall}:java.lang。空指针异常

附言。已完成所有导入。

【问题讨论】:

  • @Isuru : 我需要将 TextView 放入 View 类中 您不能将 TextView(或任何类型的 View)放入另一个 ViewViewGroup。你的 SnowFall Activity(正如你提到的)的“内容”是一个 SnowFallView,它扩展了 View。根本做不到。
  • 好的,我可以将视图更改为视图组,它会给现有动画带来错误吗?
  • 我的问题没有解决方案,但感谢所有帮助过的人!

标签: java android android-layout view textview


【解决方案1】:

是的,您可以像这样在运行时添加TextView。这假设MyClassandroid.content.Context 的子类。我不确定您在问题的第二部分中询问的是什么布局。你已经有了容器布局(linearLayout)。

【讨论】:

    【解决方案2】:
    countDownView = new TextView(context);
    countDownView.setText("It's working");
    addContentView(countDownView, null);
    

    我认为你的问题出在这里:

    addContentView(countDownView, null);
    

    你应该使用

    addView(countDownView)
    

    而不是addContentView

    【讨论】:

    • 然后我得到一个名为 The method addView(TextView) is undefined for the type SnowFall.SnowFallView
    • 是的,你的权利。那是因为 SnowFallView 继承自 View 而不是 ViewGroup。 addContentView 是 Window 类的方法。它需要有效的布局参数而不是 null
    • 尝试扩展 ViewGroup 而不是 View,并 ovveride onLayout 以便为每个孩子在 ViewGroup 中提供一个位置
    【解决方案3】:

    我不确定这是否 100% 正确,但您需要这样做...

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        SnowFallView snowFallView = new SnowFallView(this);
        TextView txt1 = new TextView(this);
        linearLayout.addView(snowFallView);
        linearLayout.addView(txt1);
        setContentView(linearLayout);
        snowFallView.setBackgroundDrawable(getResources().getDrawable(R.drawable.background_image));
    }
    

    同时从SnowFallView 类中删除这些行。

    countDownView = new TextView(context);
    countDownView.setText("It's working");
    addContentView(countDownView, null);
    

    【讨论】:

    • 如何添加测试?我试过 txt1.setText("Testing");它没有显示。 TextView 不显示。
    • SnowFallView 可能占据了全屏,TextView 没有空间。尝试更改addView(...) 的顺序,以便首先添加txt1。您可能需要调整 snow_flake 可绘制对象的大小(边界)以减小高度。
    • 是的,这就是问题所在,当我更改它显示的顺序时,就像这样。 i.imgur.com/uWuqO.jpg 但我需要将视图添加到另一个视图的顶部。我该怎么做?
    • @Isuru :当您说 top 时,您的意思是您想要屏幕顶部的 SnowFallView 和下方的 TextView(与您显示的图像相反) )?或者你的意思是TextView覆盖SnowFallView?您可以通过使用FrameLayout 而不是LinearLayout 并在最后添加TextView 来做到这一点。 FrameLayout 将“分层”添加到其中的视图。您需要在TextView 上设置垂直padding 才能正确定位它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多