[英]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.