简体   繁体   中英

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

I want to change the size of edittext(only numbers are input) according to screensize as it has two spinners in same line of edittext horizontally,so in some screens i cannot see second spinner or part of second spinner.

So if i can decrease size of edittext depending on screen it will include all three (edittext and 2 spinners ) on same line.

I don't want to keep edittext fixed with smaller width as its hint gets cut,so depending on screensize i should also be able to change text size and hint size. and how to change text size depending on edittext width?

图片来自安卓工作室

Below is my activity_main.xml file

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

What you can do is, make both of your spinner width "wrap_content" and make edit text with match constraint property that is width 0dp with left and right constraints matched, this way it will take all the remaining space.

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

You use LinearLayout as the most logical solution. Make sure the width value is "0dp", you will assign the value of layout_weight for each object. These values work as interdependent ratio.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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