Place view inside FrameLayout in Android

前端 未结 5 1294
再見小時候
再見小時候 2020-12-31 17:41

I want to add a view inside a FrameLayout programmatically and to place it in a specific point within the layout with a specific width and height. Does FrameLayout support t

5条回答
  •  悲&欢浪女
    2020-12-31 18:14

    The following example (working code) shows how to place a view (EditText) inside of a FrameLayout. Also it shows how to set the position of the EditText using the setPadding setter of the FrameLayout (everytime the user clicks on the FrameLayout, the position of the EditText is set to the position of the click):

    public class TextToolTestActivity extends Activity{
        FrameLayout frmLayout;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            frmLayout = (FrameLayout)findViewById(R.id.frameLayout1);
            frmLayout.setFocusable(true);
            EditText et = new EditText(this);
    
            frmLayout.addView(et,100,100);
            frmLayout.setOnTouchListener(new OnTouchListener() {
    
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    Log.i("TESTING","touch x,y == " + event.getX() + "," +     event.getY() );
                    frmLayout.setPadding(Math.round(event.getX()),Math.round(event.getY()) , 0, 0);
                return true;
            }
        });
    
    }
    

    }

    main.xml

    
    
        
        
    
    
    

提交回复
热议问题