动态创建TextView时,第二个TextView不会出现

时间:2015-07-09 16:12:32

标签: android android-linearlayout layoutparams

我使用下面的代码在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来定位它们。完成后,我会把这些线路拿出来。第一个工作,看起来很好,但第二个没有出现,我不明白为什么。

1 个答案:

答案 0 :(得分:0)

我认为它在屏幕外尝试较低的保证金和头寸价值 例如:

    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);