简体   繁体   English

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

[英]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.因此,如果我可以根据屏幕减小 edittext 的大小,它将包括同一行上的所有三个(edittext 和 2 个微调器)。

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.我不想让edittext固定在较小的宽度上,因为它的提示被剪切了,所以根据屏幕大小,我也应该能够改变文本大小和提示大小。 and how to change text size depending on edittext width?以及如何根据edittext宽度更改文本大小?

图片来自安卓工作室

Below is my activity_main.xml file下面是我的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>

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.您可以做的是,使您的微调器宽度都为“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" />

You use LinearLayout as the most logical solution.您使用 LinearLayout 作为最合乎逻辑的解决方案。 Make sure the width value is "0dp", you will assign the value of layout_weight for each object.确保宽度值为“0dp”,您将为每个 object 分配 layout_weight 的值。 These values work as interdependent ratio.这些值作为相互依赖的比率起作用。

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

相关问题 如何根据屏幕尺寸更改按钮尺寸? - How to change button size depending on screen size? Android:根据设备的屏幕尺寸更改方向更改选项 - Android: Change orientation change options depending on screen size of device Android 根据适配器中的屏幕旋转/宽度改变高度 - Android change height depending on screen rotation /width in Adapter 如何防止输入的文本大小改变Edittext的宽度? - How to prevent the width of an Edittext change by the size of the text entered? 如何根据手机屏幕尺寸改变敌人的尺寸,速度等? - How to change enemy size, speed, etc depending on mobile screen size? 如何在Android中为不同的屏幕尺寸宽度设置textview宽度? - How to set textview width for different screen size width in Android? 如何根据屏幕尺寸设置单选按钮的宽度? (机器人) - How can I set the width of radio buttons to change with regards to screen size? (android) 强制android屏幕方向取决于屏幕大小 - Force android screen Orientation depending on screen size Android EditText布局宽度全尺寸 - Android EditText Layout Width Full size Android根据屏幕尺寸更改图像大小? - Android Changing image size depending on Screen Size?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM