【问题标题】:I have a problem with the button position in Android Studio我在 Android Studio 中的按钮位置有问题
【发布时间】:2021-12-04 03:31:02
【问题描述】:

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

例子:

Design

Simulator

我的代码:

<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 />

【问题讨论】:

    标签: android xml android-studio android-layout


    【解决方案1】:

    Button 中的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>
    

    【讨论】:

      猜你喜欢
      • 2019-02-22
      • 2019-10-07
      • 1970-01-01
      • 1970-01-01
      • 2016-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多