Screen dimensions not effective?

0

I apply the form to obtain the dimensions of the screen, but something fails because apparently it is not fulfilled in all the resolutions. If for example I define the points for a small resolution, those points are slid down as I run the application in higher resolutions. In the drawing I gave the example of how the points appear in an Emulator 2.7 QVGA API 25 (small) and in a Nexus 7 API 25 (xlarge). I can not conclude that Android Studio has a bug. Please, help me see the problem since I have searched and raised it but I have not received a response. This is my xml:     

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="15"
    android:orientation="vertical">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:components="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/layout1">

    </RelativeLayout>
</ScrollView>

And this is the java:

public class MainActivity extends AppCompatActivity {

View pulsado;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    //Calcula width y height de la pantalla
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int width = metrics.widthPixels; // ancho absoluto en pixels
    int height = metrics.heightPixels; // alto absoluto en pixels

    // Coloca Buttons
    RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout1); // id del XML
    for (int i = 1; i < 5; i++) {
        RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        // Tamaño de los Buttons
        rel_btn.width = 4 * width / 100;
        rel_btn.height = 4 * width / 100;
        // Posición de los Buttons
        switch (i) {
            case 1:  // superior izquierda
                rel_btn.leftMargin = 0;
                rel_btn.topMargin = 0;
                break;
            case 2: // superior derecha
                rel_btn.leftMargin = Math.round((float) (96 * width / 100));
                rel_btn.topMargin = 0;
                break;
            case 3:  // inferior izquierda
                rel_btn.leftMargin = 0;
                rel_btn.topMargin = Math.round((float) (80 * height / 100));
                break;
            case 4:  // inferior derecha
                rel_btn.leftMargin = Math.round((float) (96 * width / 100));
                rel_btn.topMargin = Math.round((float) (80 * height / 100));
                break;
        }
        Button btnTag = new Button(this);
        btnTag.setLayoutParams(rel_btn);
        btnTag.setBackgroundColor(Color.BLUE);
        btnTag.setId(0 + i);
        btnTag.setOnClickListener(prueba);
        layout.addView(btnTag);
    }
}

    private View.OnClickListener prueba = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (pulsado != null) {
                Button button1 = (Button) pulsado;
                button1.setBackgroundColor(Color.BLUE);
            }
            Button button2 = (Button) view;
            GradientDrawable drawable = new GradientDrawable();
            drawable.setShape(GradientDrawable.RECTANGLE);
            drawable.setStroke(8, Color.RED);
            button2.setBackgroundDrawable(drawable);
            pulsado = view;
        }
    };
}
    
asked by F. Alvarado 24.06.2018 в 21:16
source

0 answers