当我创建一个按钮时,它在不同的手机中变得不同,为什么......?

when i'm creating a button it become different size in different mobiles why...?

我是 android 的新手,当我创建按钮时,它在不同的手机中变得不同,请帮助我。

<LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center_horizontal">
        <Button
            android:layout_width="wrap_content"
            android:gravity="center"
            android:width="300sp"
            android:layout_height="wrap_content"
            android:text="chennai to coimbatore"/>
    </RelativeLayout>
</LinearLayout>

您正在使用独立于设备的 sp 单位,但会根据用户的字体大小首选项进行缩放

只需使用dp代替

不同的手机有不同的密度,因此您可以根据密度选择小号按钮

像下面这样构造您的按钮。删除android:width="300sp",不需要:

<LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center_horizontal">
        <Button         
          android:layout_width="300dp"
          android:gravity="center"
          android:layout_height="wrap_content"
          android:text="chennai to coimbatore"/>
    </RelativeLayout>
</LinearLayout>

使用 dp 而不是 sp。 根据文档 (https://developer.android.com/guide/practices/screens_support.html) "you should prefer the sp (scale-independent pixel) to define text sizes. The sp scale factor depends on a user setting and the system scales "