【问题标题】:Typing in edittext increase its space and decreases the other edittext space键入edittext会增加其空间并减少其他edittext空间
【发布时间】:2017-05-11 05:43:03
【问题描述】:

我有一个带有两个文本视图和两个编辑文本的线性布局。问题是,每当我开始在任何一个 editText 中写作时,另一个 editText 的空间开始变小,并且随着键入的继续,焦点的 editText 空间会增加。

我的 XML 代码是:

   <LinearLayout
                android:id="@+id/l6"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/l5"
                android:layout_margin="10dp"
                android:orientation="horizontal"
                android:padding="2dp"
                android:weightSum="4">

                <TextView
                    android:id="@+id/txtOverAddress"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.2"
                    android:text="Oversease Address: " />

                <EditText
                    android:id="@+id/edtOverAddress"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="12sp"
                    android:layout_weight="2.6"
                    android:inputType="text"
                    android:padding="5dp" />

                <TextView
                    android:id="@+id/txtReffBy"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_weight="0.2"
                    android:text="Reffered By: " />

                <EditText
                    android:id="@+id/edtReffBy"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:textSize="12sp"
                    android:inputType="text"
                    android:padding="5dp" />

            </LinearLayout>

我不知道我在哪里做错了。需要帮忙!提前致谢。

【问题讨论】:

    标签: android android-linearlayout android-xml android-layout-weight


    【解决方案1】:

    我对您的代码做了一些更改,但我不确定您的要求是什么 1. 如果文本超出 Edittext 的宽度,你想移动下一行的文本吗? 2. 或者超过Edittext的宽度要横向滚动吗?

    我已经完成了给定的 EditText 中的两件事。第一个移动到新行,第二个将滚动

    <LinearLayout android:id="@+id/l6"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/l5"
    android:layout_margin="10dp"
    android:orientation="horizontal"
    android:padding="2dp"
    android:weightSum="4"
    xmlns:android="http://schemas.android.com/apk/res/android">
    
        <TextView
            android:id="@+id/txtOverAddress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:text="Oversease Address: " />
    
        <EditText
            android:id="@+id/edtOverAddress"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:layout_weight="2.6"
            android:text="Hello I am doing good. Hope you are also doing well"
            android:inputType="textMultiLine"
            android:padding="5dp" />
    
        <TextView
            android:id="@+id/txtReffBy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_weight="0.2"
            android:text="Reffered By: " />
    
        <EditText
            android:id="@+id/edtReffBy"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="12sp"
            android:inputType="text"
            android:text="Hello I am doing good. Hope you are also well"
            android:padding="5dp" />
    
    </LinearLayout> 
    

    希望这会有所帮助。

    【讨论】:

    • 如果超过Edittext的宽度,我想水平滚动文本。让我应用你的答案。
    • 在这种情况下,您将使用 textMultiLine 选项和 EditText。就像我在 First EditText 中使用的一样。您可以使用 android:maxLines="1" 来限制行数,而不是 Multiline
    【解决方案2】:

    如果你可以设置android:layout_weight,那么基于线性布局方向你可以设置android:layout_width="0dp"

       <LinearLayout
                    android:id="@+id/l6"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/l5"
                    android:layout_margin="10dp"
                    android:orientation="horizontal"
                    android:padding="2dp"
                    android:weightSum="4">
    
                    <TextView
                        android:id="@+id/txtOverAddress"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.2"
                        android:text="Oversease Address: " />
    
                    <EditText
                        android:id="@+id/edtOverAddress"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:textSize="12sp"
                        android:layout_weight="2.6"
                        android:inputType="text"
                        android:padding="5dp" />
    
                    <TextView
                        android:id="@+id/txtReffBy"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="8dp"
                        android:layout_weight="0.2"
                        android:text="Reffered By: " />
    
                    <EditText
                        android:id="@+id/edtReffBy"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:textSize="12sp"
                        android:inputType="text"
                        android:padding="5dp" />
    
                </LinearLayout>
    

    【讨论】:

    • 当我做 android:layout_width="0dp" 时,我的 textViews 文本是垂直的。
    • 您只能将 android:layout_width="0dp" 用于 EditText。尝试让我们知道。我也发布了答案,请检查一下
    • 这是根据您的要求...您想要什么?如果您只想修复edittext,则仅将宽度0dp放入edittext ...
    • @SummvedJain 谢谢。这对我帮助很大!
    【解决方案3】:

    在权重总和为 4 的父线性布局中创建两个权重为 2 的线性布局。然后在每个宽度为 0 dp 的线性布局中创建 textview 和 edittext。

    【讨论】:

    • @Bhupat Bheda 给出的代码是正确的,如果您不需要垂直文本视图中的文本,您可以在 tetxview 中添加这两行。否则您将不得不最小化编辑文本权重大小并增加文本视图权重。 android:singleLine="true" android:ellipsize="end"
    • 谢谢,有帮助!
    猜你喜欢
    • 1970-01-01
    • 2015-06-17
    • 1970-01-01
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 2014-09-07
    • 2016-03-03
    相关资源
    最近更新 更多