javaandroidbuttonandroid-button

Android Studio Button not aligned with the rectangular box


When I dragged a button into the design mode, the button is not centered within the rectangle box as shown in the picture. As a result, when I added constraints to the bottom, left, and right, the button disappears, only leaving the rectangle box visible. Is there anyway to resolve it? enter image description here

The original code in the xml file is:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:rotationX="4"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        tools:layout_editor_absoluteX="207dp"
        tools:layout_editor_absoluteY="173dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • Remove android:rotationX="4" from ConstraintLayout. For more info, please follow this link.

    Tip: while creating UI using drag-and-drop may be easier, it's best to review the generated code as it might result in a less optimal UI.