将 TextView 居中,同时将其保持在按钮右侧?
我很确定我以前曾经这样做过,但我忘记了如何做。
问题是这样的: 我有一个按钮和一个文本视图,我希望文本视图居中,而按钮位于左侧。
没问题?只需将它们放在相对布局中,使文本视图居中,按钮对齐父左。
但现在我要动态更改文本,因此它可以写在按钮顶部!我只需在文本视图上添加 toRightOf="@id/button" 即可。不,现在它不再居中了。
我希望我可以提供屏幕截图,但似乎计算机内存不足,无法提供屏幕截图。
这里有一些代码: http://pastebin.com/3N70Vjre (因为我无法粘贴 xml. ..?)
<RelativeLayout
android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<Button
android:id="@+id/leftbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="text!"
/>
<TextView
android:id="@+id/toptext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:layout_toRightOf="@id/leftbutton"
android:layout_centerInParent="true"
android:textSize="16sp"
android:textStyle="bold"
android:text="Text!"
android:singleLine="true"
/>
</RelativeLayout>
I'm pretty sure I've done this before, but I've forgotten how.
Here's the problem:
I've got a button and a textview, and I want the textview to be centered, while the button is on the left side.
No problem? Just put them in a relativelayout, make the textview centerinparent, and the button alignparentleft.
But now I'm going to dynamically change the text, so it can potentially be written on top of the button! I'll just add toRightOf="@id/button" on the textview. No, now it's no longer centered.
I wish I could provide a screenshot, but it seems the computer is out of memory and can't do that.
Here's some code: http://pastebin.com/3N70Vjre (Since I can't paste xml...?)
<RelativeLayout
android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<Button
android:id="@+id/leftbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="text!"
/>
<TextView
android:id="@+id/toptext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:layout_toRightOf="@id/leftbutton"
android:layout_centerInParent="true"
android:textSize="16sp"
android:textStyle="bold"
android:text="Text!"
android:singleLine="true"
/>
</RelativeLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个(不幸的是我在工作,所以无法跳到 Eclipse 来获取一些代码)-
这应该使 TextView 的文本在 Button 之后居中,但它不会使 TextView 本身居中。您可以通过用容器(线性/相对布局)替换 TextView 并在布局而不是 TextView 上执行与上面相同的方法来实现此目的。然后,您可以将 TextView 放入容器内,并将容器的重力设置为“中心”。
希望这有助于为您指明正确的方向:)
Try this (unfortunately I'm at work so can't jump into Eclipse to get you some code) -
This should center the text of the TextView after the Button, though it will not center the TextView itself. You can accomplish that by replacing the TextView with a container (Linear/Relative Layout) and doing the same method as above on the Layout instead of the TextView. You would then put your TextView inside the container and set the container's gravity to "center".
Hope this helps point you in the right direction :)
您可以尝试这个(伪代码):
您可能必须将 LinearLayout 设置为 width="fill_parent"。不确定这是否有效。随后您可以尝试此处列出的一些操作: http://thinkandroid.wordpress.com/2010/01/14/how-to-position-views-properly-in-layouts/
You can try this (pseudo-code):
You might have to have the LinearLayout as width="fill_parent". Not sure if that will work nor not. You can subsequently try some of the things listed here: http://thinkandroid.wordpress.com/2010/01/14/how-to-position-views-properly-in-layouts/
尝试首先声明 TextView,然后将按钮与文本视图的左侧对齐。请记住,如果 TextView 变得太宽,您可能会遇到问题。
编辑:我明白了,所以你正在尝试做一些类似于 iPhone 带有后退/下一步按钮的标题(无论如何类似)。尝试一下这个修改。不过,我仍然相信,如果 TextView 变得足够大以点击按钮,您将会遇到问题。
试试这个 FrameLayout 吧。这可能会做更多你所期望的事情:
Try declaring the TextView first, then aligning the button to the left of the text view. Keep in mind you may run into issues if the TextView becomes too wide.
EDIT: I see, so you're trying to do something sort of like the iPhone's header with back/next buttons (similar anyway). Try this modification. I still believe you're going to run into issues if the TextView gets large enough to hit the Button, though.
Try this FrameLayout instead. This may do more what you're expecting: