I came up with what I’ve come up with, I need the layout_marginTop be exactly half the screen and here’s the question. If I set half of the “in my opinion” screen in the XML file, let's say 250dp then on the device with a different resolution it will not be half. That's what I want to do, I can programmatically get the height of the screen to divide it by 2 and set the resulting value as layout_marginTop then it will be exactly half regardless of the screen resolution ... But there is no such function setMarginTop() . How can this be done?

  • You have fundamentally changed the question to which you have already been answered. So you can not do. If you have a new question - ask it separately. - Vladyslav Matviienko

2 answers 2

public void setMargins (int left, int top, int right, int bottom)

Sets the indents in pixels. setMargins

    The problem is that you apply the setMargins() method directly to the resulting object, which this method does not have.

    The android.view.ViewGroup.MarginLayoutParams class has this method. Inherit him:
    FrameLayout.LayoutParams , LinearLayout.LayoutParams and RelativeLayout.LayoutParams .

    Try this:

     //Получаете объект, который вы хотите изменить AutoFitTextureView mTextureView = (AutoFitTextureView)findViewById(R.id.texture); LinearLayout.LayoutParams param = new LinearLayout.LayoutParams; param.setMargins(10,15,20,35);//left, top, right, bottom mTextureView.setLayoutParams(param); 

    And if anything, there is still a wonderful property layout_gravity="center_horizontal"