【问题标题】:When the font size is changed in the phone, text, buttons go out of the screen当手机中的字体大小发生变化时,文本、按钮会离开屏幕
【发布时间】:2018-03-10 06:29:17
【问题描述】:

我遇到了一个问题,我不知道在哪里寻找信息,如果您知道如何解决我的问题,请向我提供帮助。 我在另一部手机上尝试了我的应用程序,它是具有相同 api 的同一类型手机。问题是其中一部手机的字体更大,图标/按钮离开屏幕。 有没有办法将我的应用程序设置为以特定类型的字体大小打开(例如:Tiny、extra small 等)? 提前致谢!

稍后编辑:


这是我的 xml 布局的代码:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center"
tools:context="****"
android:focusableInTouchMode="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimaryDark"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="10dp">

    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="@string/login_first_dialog"
        android:textColor="@color/textColorMainActivity"
        android:textSize="30dp"
        android:gravity="center"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="@string/login_second_dialog"
        android:textColor="@color/textColorMainActivity"
        android:textSize="15dp"
        android:gravity="center"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal"
        android:layout_gravity="center">

        <Button
            android:id="@+id/ButtonPhoneNumberID"
            android:drawableStart="@drawable/ic_phone_black_24dp"
            android:background="@drawable/login_button_style"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:text="@string/phone_button"
            android:textColor="@color/textColorMainActivity"/>
        <Button
            android:id="@+id/ButtonHelpLoginID"
            android:drawableStart="@drawable/ic_help_outline_black_24dp"
            android:background="@drawable/login_button_style"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:text="@string/complaints_button"
            android:textColor="@color/textColorMainActivity"/>
    </LinearLayout>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_marginBottom="10dp">
    <EditText
        android:id="@+id/EditTextLogInEnterEmailID"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:hint="@string/email_hint"
        android:drawableStart="@drawable/ic_email_black_24dp"
        android:drawablePadding="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_marginStart="5dp"/>
    <EditText
        android:id="@+id/EditTextLogInEnterPasswordID"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="@string/password_hint"
        android:drawableStart="@drawable/ic_lock_outline_black_24dp"
        android:drawablePadding="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_marginStart="5dp"/>
    <Button
        android:id="@+id/LogInButtonID"
        android:background="@drawable/login2_button_style"
        android:drawableEnd="@drawable/ic_send_black_24dp"
        android:drawablePadding="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#808e85"
        android:layout_marginBottom="10dp"
        android:text="@string/log_in_button"/>

    <Button
        android:id="@+id/SignUpButtonID"
        android:background="@drawable/login2_button_style"
        android:layout_marginTop="10dp"
        android:drawableEnd="@drawable/ic_person_add_black_24dp"
        android:drawablePadding="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/sign_up_button"
        android:textColor="#808e85"
        android:layout_marginBottom="20dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimaryDark"
        android:orientation="vertical"
        android:gravity="center">
        <ProgressBar
            android:id="@+id/ProgressBarLogInID"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:visibility="gone"
            />
    </LinearLayout>

</LinearLayout>

【问题讨论】:

  • 能否提供您的xml布局代码?
  • 是的,当然,我现在就更新它
  • 您正在为视图设置固定大小的宽度.. 即使您不在同一设备上更改大小,您也会在不同设备上遇到问题.. 尝试使用 wrap_content 或 @ 987654323@
  • 好的,谢谢!我会这样做的:)!

标签: android android-layout font-size


【解决方案1】:

看起来您正在使用sp 单位来指示Button TextView 等的字体大小...改为使用dp

例如: 你可能在你的布局 xml 文件中有这个 text_size="10sp" 改成10dp

【讨论】:

  • 这不是问题,即使我将 sp 更改为 dp(老实说,我不认为它会改变某些东西)当我更改手机上的字体大小时,文字占据了大部分屏幕和按钮往往会“离开屏幕”。所以,没有任何改变
  • sp 会根据用户的字体大小偏好进行缩放,因此将 sp 更改为 dp 应该会使文本保持不变。发布截图,让我们更清楚地了解问题所在
【解决方案2】:

不要TextView中的android:textSize属性使用“dp”。
使用“sp”代替文本大小,例如android:textSize="30sp"
您可以将“dp”用于任何与边距或填充相关的内容。
查看此链接以获取更多说明:What is the difference between "px", "dip", "dp" and "sp"?

【讨论】:

  • 我第一次制作布局时没有使用 dp 作为文本大小,我更改它们只是因为 Eddie 告诉我尝试看看它是否有效。最初我用 sp 来表示文字大小
  • 好的,顺便问一下,你试过ConstraintLayout了吗?因为有 4 个嵌套的 LinearLayout 可能会对您的应用的性能产生负面影响。
猜你喜欢
  • 2021-02-13
  • 2023-02-23
  • 1970-01-01
  • 2010-10-24
  • 1970-01-01
  • 2013-06-20
  • 2013-04-06
  • 1970-01-01
  • 2021-07-19
相关资源
最近更新 更多