【问题标题】:When creating TextView dynamically the second TextView is not appearing动态创建 TextView 时,第二个 TextView 没有出现
【发布时间】:2015-07-10 00:33:49
【问题描述】:

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

【问题讨论】:

    标签: android android-linearlayout layoutparams


    【解决方案1】:

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

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

    【讨论】:

    • 成功了,我忘了Linear一个接一个,谢谢
    猜你喜欢
    • 2019-12-16
    • 1970-01-01
    • 1970-01-01
    • 2016-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-17
    • 2016-06-03
    相关资源
    最近更新 更多