簡體   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