具有正确重力和单行的提示和文本视图
我打开了一个错误,但我想知道是否有人遇到过这个问题并知道解决方法。 如果您定义一个带有提示的文本视图,请给它正确的重力(android:gravity =“right”),然后如果您定义android:singleLine = true或android:maxLines =“1”或android:scrollHorizonatally =“true”你没有看到提示。删除右侧重力会将提示返回到左侧,删除我上面提到的所有树参数会将提示放在右侧。我希望我的提示在右侧,但我需要一条水平线...
这是不显示提示的示例布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<EditText android:layout_width="fill_parent"
android:layout_gravity="center_vertical|right"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:textSize="16sp"
android:paddingRight="5dp"
android:id="@+id/c"
android:gravity="right"
android:hint="hello!!!"
android:scrollHorizontally="true"
android:maxLines="1"
android:singleLine="true"/>
</LinearLayout>
我检查了 1.6 和 2.1 模拟器,它再现了 100%,我很确定这是一个错误,我没有看到单行和提示之间的联系......更重要的是,提示在 TextView 中拥有自己的布局(mLayout 和 mHintLayout 都存在,在 onDraw 中,如果文本长度为 0 mHintLayout 如果 mHint 不为 null被使用)。
I've opened a bug but i was wondering if anyone encountered this issue and knows a workaround.
If you define a text view with a hint inside it, give it right gravity (android:gravity="right") then if you define android:singleLine=true or android:maxLines="1" or android:scrollHorizonatally="true" you don't see the hint. removing the right gravity returns the hint to the left side, removing all the tree params i mentioned above puts the hint on the right side. i want my hint on the right, but i need a single horizontal line...
here's the sample layout that doesn't show the hint:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<EditText android:layout_width="fill_parent"
android:layout_gravity="center_vertical|right"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:textSize="16sp"
android:paddingRight="5dp"
android:id="@+id/c"
android:gravity="right"
android:hint="hello!!!"
android:scrollHorizontally="true"
android:maxLines="1"
android:singleLine="true"/>
</LinearLayout>
i checked on 1.6 and 2.1 emulators and it reproduces 100%, i'm prettysure it's a bug, i don't see the connection between single line and the hint.... what's more the hint got it's own layout in the TextView (mLayout and mHintLayout both exists, in onDraw if the text length is 0 mHintLayout if mHint is not null is used).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您尝试过
android:ellipsize="start"
吗?过去,当我希望提示和EditText
居中时,这对我来说非常有用。Did you try
android:ellipsize="start"
? This has worked great for me in the past when I've wanted my hint andEditText
to be centered.看起来你对这个问题的看法完全正确;我尝试使用您的示例布局并看到了同样的问题。我假设这是您的错误报告。
最简单的解决方案是仅更改布局,但这可能不是您想要做的。我的第一个解决方法是尝试不在 XML 中设置这三个属性中的任何一个,然后在 Java 中设置它们。如果这不起作用...
一种选择是通过扩展 EditText 类并尝试自行修复布置提示的代码来模仿提示,或者通过重写 onDraw 方法来创建提示,或者简单地通过将常规 TextView 重叠在 EditText 之上,然后您可以手动显示/隐藏它。您甚至可以让视图检查它是否为空,如果是,则将文本设置为提示文本并更改颜色。当视图获得焦点时,检查其文本是否等于您的提示,如果是,则删除文本并将颜色更改回来。
另一种可能的解决方法有点“hacky”,那就是放弃导致问题的三个属性,但尝试手动阻止创建换行符。您需要为您的 EditText 创建一个 OnKeyListener,如下所示:
您还需要调用
editText.setImeOptions(EditorInfo.IME_ACTION_NEXT)
以避免显示返回键。仍然可以通过粘贴或其他方法在文本字段中创建换行符,因此为了安全起见,您还需要在提交表单时解析并删除换行符。就水平滚动而言,这也不太可能达到您想要的效果。Looks like you're exactly right with the issue; I tried playing with your example layout and saw the same issue. I assume this is your bug report.
The easiest solution is to just change your layout, but that's probably not what you want to do. My first attempt at a work around would be to try not setting any of those three attributes in XML and then setting them in Java. If that doesn't work...
One option is to mimic the hint by either extending the EditText class and attempting to fix the code that lays out the hint yourself, or by overriding the onDraw method to create the hint, or perhaps by simply overlapping a regular TextView on top of the EditText, which you then show/hide manually. You could even have the view check if it's empty, and if so set the text to your hint text and change the color. When the view gains focus, check if its text is equal to your hint and, if so, remove the text and change the color back.
Another possible workaround that's a bit more "hacky" is to leave off the three attributes that cause problems, but try to manually prevent a newline from being created. You'd need to create an OnKeyListener for your EditText, something like this:
You would also want to call
editText.setImeOptions(EditorInfo.IME_ACTION_NEXT)
to avoid showing the return key. It still may be possible to create a newline in your text field by pasting into it or perhaps some other method, so you would also want to parse and remove newlines when the form is submitted just to be safe. This is also not likely to do what you want as far as horizontal scrolling.使用这些属性与提示和单线...你可以改变重力!
use these properties with hint and single line...u can chnge gravity!!
注意到对我来说已经足够好了。当我设置Gravity.right时,光标总是在右侧,无法放置在单词中间。
我尝试了一种不同的方法 - 当没有文本时将重力设置在右侧(或左侧,如果它适合您),并让 android 在用户输入内容后决定最佳方向
这对我有用:
create
TextWatcher
类将其用作类成员
:
在
onDestroy()
中:Noting worked good enough for me. When I set Gravity.right, the cursor was always on the right and couldn't be placed in the middle of the word.
I tried a different approach - put the set the gravity the the right when there is no text (or left, if it works for you) and let android decide the best direction once the user entered something
This worked for me:
create
TextWatcher
classuse it as class member
in
onCreate()
:in
onDestroy()
:您认为我对这个问题的解决方案怎么样?
相应的 XML 文件:
对我来说工作得很好:只有一行,没有可能的回车键,向我显示提示,当我在给出一些输入后离开该字段时,文本显示为右对齐。
What do you think about my solution to this problem?
And the corresponding XML File:
Works fine for me: just one line, no enter-key possible, shows me the hint and when I leave the field after some input was given, the text appears right-aligned.
它对我有用
当我添加:并在我的活动中添加:时
it worked with me when I added:
and in my activity i added :
当我的 TextView atrs 为:
当我尝试在该文本视图上链接文本视图或 setMovementMethod(LinkMovementMethod.getInstance()) 时,我注意到这个问题,文本就消失了。
解决了我的问题,因为我在我的应用程序中使用阿拉伯文本。
I noticed this issue when my TextView atrs are:
When I try to Linkify the textview or setMovementMethod(LinkMovementMethod.getInstance()) on that textview, the text is just gone.
solved my issue, because I use Arabic text in my app.