【问题标题】:Android wrap_content does not wrapAndroid wrap_content 不换行
【发布时间】:2014-10-02 21:51:57
【问题描述】:

这里是新手 Android 程序员。我正在制作一个带有 EditText 的 xml 布局,它的宽度应该改变以适合里面的文本——文本可以由用户或以编程方式更改。我发现当文本超过 4 个字符时,EditText 停止扩展并将多余的字符保持在屏幕外。 这是我的代码:

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.appname.MainActivity" >

    <EditText
    android:id="@+id/textview1"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_above="@+id/seekbar1"
    android:layout_alignLeft="@+id/textview2"
    android:ems="10"
    android:imeActionLabel="Done"
    android:imeOptions="actionDone"
    android:inputType="phone"
    android:text="@string/NaN" />

    //Other objects...

</RelativeLayout>

阅读有关 wrap_content 的文档后,我认为它可以满足我的需求。我弄错了吗?感谢您的帮助!

【问题讨论】:

  • 你写了 android:layout_height="wrap_content"。据我了解,您想要可扩展的宽度,而不是高度......
  • 你不能使用 android:singleLine="true" 来编辑文本吗?

标签: android xml


【解决方案1】:

首先你已经固定了edittext的宽度

android:layout_width="100dp" 这应该更改为 wrap_content。

其次,即使您这样做,它也不会起作用,因为包装内容不能那样工作。如果您必须动态更改 EditText 的大小,则必须扩展类 TextWatcher 并覆盖方法 ontextchanged() 或 afterTextChanged() 。在这种方法中,您必须根据输入的字符动态分配宽度属性。

【讨论】:

  • 谢谢,真不敢相信我错过了...如果我将初始文本设置为正确长度的占位符,wrap_content 会起作用吗?例如。如果我知道动态设置的文本长度为 6,并且我将初始文本设置为“NNNNNN”,它适合吗?
  • 您想动态更改它,所以我认为它不会起作用..尝试一下,也让我知道。顺便说一句,如果有帮助,请标记我的答案。如果你这样做,我会得到 20 分:)
猜你喜欢
  • 1970-01-01
  • 2014-11-17
  • 2010-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多