简体   繁体   English

为什么我的EditText在Android中将Width和Height设置为wrap_content后水平或垂直展开?

[英]Why does my EditText got expanded horizontally or vertically after setting Width and Height as wrap_content in android?

I'm newbie to android. 我是android的新手。 So I'm struggling with the following problem. 所以我正在努力解决以下问题。

1.after set android:layout_width = "wrap_content" in EditText, it got expanded vertically. 1.在EditText中设置android:layout_width = "wrap_content"之后,它被垂直扩展。

2.after set android:layout_width="180dp" in EditText, it got expanded horizontally. 2.在EditText中设置android:layout_width="180dp"后,它被水平扩展。

Is there anyway to set my EditText fixed? 有没有设置我的EditText固定? Ex: If am going to type a long email address in EditText, font size of the text should get shrinked based on long text instead of EditText getting expanded. 例如:如果要在EditText中键入长电子邮件地址,则文本的字体大小应基于长文本而不是EditText进行扩展而缩小。 is there any possibility to achieve this task? 有没有可能完成这项任务?

here is my code 这是我的代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"                            android:layout_width="match_parent"
android:layout_height="match_parent" 

tools:context="com.vadmin.myrx.SendReport">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Doctor Email"
    android:textStyle="bold"
    android:id="@+id/textView1"
    android:layout_marginTop="75dp"
    android:layout_marginLeft="25dp"
    android:layout_marginStart="25dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:hint="example@xyz.com"
    android:padding="3dp"
    android:background="@drawable/text_oval"
    android:id="@+id/textView2"
    android:layout_alignBottom="@+id/textView1"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Phone"
    android:textStyle="bold"
    android:id="@+id/textView3"
    android:layout_marginRight="24dp"
    android:layout_marginEnd="24dp"
    android:layout_marginTop="44dp"
    android:layout_below="@+id/textView1"
    android:layout_alignRight="@+id/textView1"
    android:layout_alignEnd="@+id/textView1" />

<EditText
    android:layout_width="180dp"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:hint="+91 0000 000 000"
    android:padding="3dp"
    android:id="@+id/textView4"
    android:layout_alignTop="@+id/textView3"
    android:layout_alignLeft="@+id/textView2"
    android:layout_alignStart="@+id/textView2"
    android:background="@drawable/text_oval"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Report"
    android:textStyle="bold"
    android:id="@+id/textView5"
    android:layout_marginTop="45dp"
    android:layout_below="@+id/textView4"
    android:layout_alignRight="@+id/textView3"
    android:layout_alignEnd="@+id/textView3" />

<Spinner
    android:layout_width="250dp"
    android:layout_height="48dp"
    android:id="@+id/spinner"
    android:spinnerMode="dropdown"
    android:background="@android:drawable/btn_dropdown"
    android:layout_alignTop="@+id/textView5"
    android:layout_alignLeft="@+id/textView4"
    android:layout_alignStart="@+id/textView4" />

   <Button
       android:layout_width="wrap_content"
       android:layout_height="40dp"
       android:text="Save"
       android:layout_marginTop="57dp"
       android:id="@+id/button"
       android:layout_below="@+id/spinner"
       android:layout_toRightOf="@+id/textView3"
       android:layout_toEndOf="@+id/textView3"
       android:textColor="#000000"
        android:background="@drawable/oval"

       />

Thanks Anita 谢谢Anita

在此输入图像描述

Use this layout instead of your layout: 使用此布局而不是您的布局:

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.vadmin.myrx.SendReport" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:orientation="horizontal"
        android:weightSum="2" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="2dp"
            android:text="Doctor Email"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textSize="15sp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2dp"
            android:layout_weight="1"
            android:hint="example@xyz.com"
            android:inputType="textWebEmailAddress"
            android:maxLength="20"
            android:padding="3dp"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:orientation="horizontal"
        android:weightSum="2" >

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:text="Phone"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textSize="15sp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2dp"
            android:layout_weight="1"
            android:hint="+91 0000 000 000"
            android:maxLength="12"
            android:inputType="phone"
            android:padding="3dp"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="2dp"
            android:text="Report"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textSize="15sp"
            android:textStyle="bold" />

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2dp"
            android:spinnerMode="dropdown" />
    </LinearLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_below="@+id/spinner"
        android:layout_gravity="center"
        android:layout_marginTop="57dp"
        android:layout_toEndOf="@+id/textView3"
        android:layout_toRightOf="@+id/textView3"
        android:text="Save"
        android:textColor="#000000" />

</LinearLayout>

I have used your XML and modified it. 我已经使用了你的XML并对其进行了修改。 Just add your images. 只需添加图片即可。 I removed it as I wasn't having them. 我删除了它,因为我没有它们。

EDIT : I have added android:inputType for editTexts as well + and put a limit on phone number (12) and email (20), you can change them according to yourself. 编辑 :我已经添加了android:inputType for editTexts +并限制了电话号码(12)和电子邮件(20),你可以根据自己改变它们。

You can try this by replacing in your layout 您可以通过替换布局来尝试此操作

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Doctor Email"
        android:textStyle="bold"
        android:id="@+id/textView1"
        android:layout_marginTop="75dp"
        android:layout_marginLeft="25dp"
        android:layout_marginStart="25dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:hint="example@xyz.com"
        android:padding="3dp"
        android:layout_toEndOf="@id/textView1"
        android:singleLine="true"
        android:background="@drawable/text_oval"
        android:id="@+id/textView2"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:text="" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Phone"
        android:textStyle="bold"
        android:id="@+id/textView3"
        android:layout_marginRight="24dp"
        android:layout_marginEnd="24dp"
        android:layout_marginTop="44dp"
        android:layout_below="@+id/textView1"
        android:layout_alignRight="@+id/textView1"
        android:layout_alignEnd="@+id/textView1" />

    <EditText
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:hint="+91 0000 000 000"
        android:padding="3dp"
        android:layout_toEndOf="@id/textView3"
        android:singleLine="true"
        android:id="@+id/textView4"
        android:layout_alignTop="@+id/textView3"
        android:layout_alignLeft="@+id/textView2"
        android:layout_alignStart="@+id/textView2"
        android:background="@drawable/text_oval"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Report"
        android:textStyle="bold"
        android:id="@+id/textView5"
        android:layout_marginTop="45dp"
        android:layout_below="@+id/textView4"
        android:layout_alignRight="@+id/textView3"
        android:layout_alignEnd="@+id/textView3" />

    <Spinner
        android:layout_width="250dp"
        android:layout_height="48dp"
        android:id="@+id/spinner"
        android:spinnerMode="dropdown"
        android:background="@android:drawable/btn_dropdown"
        android:layout_alignTop="@+id/textView5"
        android:layout_alignLeft="@+id/textView4"
        android:layout_alignStart="@+id/textView4" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:text="Save"
        android:layout_marginTop="57dp"
        android:id="@+id/button"
        android:layout_below="@+id/spinner"
        android:layout_toRightOf="@+id/textView3"
        android:layout_toEndOf="@+id/textView3"
        android:textColor="#000000"
        android:background="@drawable/oval"

        />

Replace your EditText with below code: 用以下代码替换EditText

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:hint="example@xyz.com"
    android:padding="3dp"
    android:background="@drawable/text_oval"
    android:id="@+id/textView2"
    android:layout_alignBottom="@+id/textView1"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:singleLine="true"
    android:ems="10"
    android:layout_toRightOf="@+id/textView1"
    android:layout_toEndOf="@+id/textView1"
    />

Setting the following property on the TextView fixed this weird issue for me. 在TextView上设置以下属性为我修复了这个奇怪的问题。 This should be the default behaviour of the textview in my opinion. 在我看来,这应该是textview的默认行为。

android:imeOptions="flagNoExtractUi"

Libraries For Developers中,您将在StackOverflow中找到多个AutoScaleTextView变体和几个代码片段来实现此目的(如果我理解正确的话)

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

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