繁体   English   中英

动态创建TextView时,第二个TextView没有出现

[英]When creating TextView dynamically the second TextView is not appearing

我正在使用以下代码在LinearLayout中创建两个TextView。 当按屏幕的指定区域时,我以这种方式使用它们来捕获触摸事件

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    LinearLayout layOut = (LinearLayout)findViewById(R.id.loginView);

    LinearLayout.LayoutParams loginParams = new LinearLayout.LayoutParams(520, 70);
    loginParams.setMargins(120, 670, 0, 0);
    TextView loginBttn = new TextView(this);
    layOut.addView(loginBttn);
    loginBttn.setBackgroundColor(Color.GREEN);
    loginBttn.setLayoutParams(loginParams);
    loginBttn.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            // Code touch events
        }
    });

    LinearLayout.LayoutParams forgotParams = new LinearLayout.LayoutParams(370, 40);
    forgotParams.setMargins(150, 770, 0, 0);
    TextView forgotBttn = new TextView(this);
    layOut.addView(forgotBttn);
    forgotBttn.setBackgroundColor(Color.GREEN);        
    forgotBttn.setLayoutParams(forgotParams);
    forgotBttn.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            // Code touch events
        }
    });

我正在使用Green来定位它们。 完成后,我将删除这些行。 第一个工作正常,看起来不错,但是第二个没有出现,我看不出为什么。

我认为它在屏幕外尝试使用较低的边距和位置值,例如:

    LinearLayout.LayoutParams loginParams = new LinearLayout.LayoutParams(50, 50);
    loginParams.setMargins(10, 10, 0, 0);
    TextView loginBttn = new TextView(this);
    layOut.addView(loginBttn);
    loginBttn.setBackgroundColor(Color.GREEN);
    loginBttn.setLayoutParams(loginParams);


    LinearLayout.LayoutParams forgotParams = new LinearLayout.LayoutParams(10, 10);
    forgotParams.setMargins(20, 20, 0, 0);
    TextView forgotBttn = new TextView(this);
    layOut.addView(forgotBttn);
    forgotBttn.setBackgroundColor(Color.GREEN);        
    forgotBttn.setLayoutParams(forgotParams);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM