繁体   English   中英

如何根据 android 中的屏幕尺寸更改编辑文本宽度?

[英]How to change edittext width depending on screen size in android?

我想根据屏幕大小更改编辑文本的大小(仅输入数字),因为它在同一行编辑文本中水平有两个微调器,因此在某些屏幕中我看不到第二个微调器或第二个微调器的一部分。

因此,如果我可以根据屏幕减小 edittext 的大小,它将包括同一行上的所有三个(edittext 和 2 个微调器)。

我不想让edittext固定在较小的宽度上,因为它的提示被剪切了,所以根据屏幕大小,我也应该能够改变文本大小和提示大小。 以及如何根据edittext宽度更改文本大小?

图片来自安卓工作室

下面是我的activity_main.xml文件

        <?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="36dp"
        android:layout_marginTop="70dp"
        android:ems="10"
        android:hint="Storage Vessel Volume"
        android:inputType="number"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        app:layout_constraintBottom_toBottomOf="@+id/editText"
        app:layout_constraintStart_toEndOf="@+id/editText" />

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        app:layout_constraintBottom_toBottomOf="@+id/editText"
        app:layout_constraintStart_toEndOf="@+id/spinner" />
</androidx.constraintlayout.widget.ConstraintLayout>

您可以做的是,使您的微调器宽度都为“wrap_content”,并使用宽度为 0dp 且左右约束匹配的匹配约束属性编辑文本,这样它将占用所有剩余空间。

<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
    android:id="@+id/editText"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="36dp"
    android:layout_marginTop="70dp"
    android:ems="10"
    android:hint="Storage Vessel Volume"
    android:inputType="number"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/spinner"
    app:layout_constraintTop_toTopOf="parent" />

<Spinner
    android:id="@+id/spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="10dp"
    app:layout_constraintEnd_toStartOf="@+id/spinner2"
    app:layout_constraintBottom_toBottomOf="@+id/editText"
    />

<Spinner
    android:id="@+id/spinner2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="10dp"
    app:layout_constraintBottom_toBottomOf="@+id/editText"
    app:layout_constraintEnd_toEndOf="parent" />

您使用 LinearLayout 作为最合乎逻辑的解决方案。 确保宽度值为“0dp”,您将为每个 object 分配 layout_weight 的值。 这些值作为相互依赖的比率起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM