我有一个问题,在Android工作室的按钮位置



我在Android Studio中制作应用程序,我的问题是按钮上的位置。在布局中,位置是正确的,但在模拟器中,按钮在左上角。

的例子:

设计模拟器

我代码:

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F10000"
android:onClick="onClick"
android:text="INICIO"
android:textColor="#000000"
tools:layout_editor_absoluteX="161dp"
tools:layout_editor_absoluteY="622dp />

tools关键字在你的按钮只是显示在布局中,没有更多的,为定义按钮的位置,你应该依靠你的按钮父,例如,如果它的LinearLayout,你可以改变按钮的位置给它android:layout_gravity

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/parent">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F10000"
android:onClick="onClick"
android:text="INICIO"
android:textColor="#000000"
android:layout_gravity="center" />
</LinearLayout>