Android 水平 LinearLayout 在 TextView 中带有换行文本
我观察到了layout_weight 的行为,但我无法解释。以下是一个简单的示例:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="This is a very very very very very very very very very very very very very very very long string."
android:layout_weight="1"
/>
<View
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center_vertical"
android:background="#ffffffff"
/>
</LinearLayout>
在 QVGA 显示器中,TextView 环绕文本。白色方块显示在文本右侧。
但是,如果我从 TextView 中删除 android:layout_weight="1",则 TextView 现在会占据整个显示宽度。白色方块不再显示。
- 为什么TextView中的layout_weight会影响白色方块的显示与否?白色背景的 View 不应该首先分配 32dpx32dp 吗? (如果视图是任何其他类型 - ImageView 或 TextView,则没有区别)。
- 我正在解决的问题是,我希望白色方块始终显示在 TextView 的右侧(无论文本是否换行),但我不希望 TextView 和白色方块之间有任何空白。 (如果我将 android:layout_weight="1" 添加到 TextView,那么如果文本未换行,则会出现间隙。)
任何帮助将不胜感激!
I've observed a behavior with layout_weight that I can't explain. The following is a trivial example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="This is a very very very very very very very very very very very very very very very long string."
android:layout_weight="1"
/>
<View
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center_vertical"
android:background="#ffffffff"
/>
</LinearLayout>
In a QVGA display, the TextView wraps the text. The white square is displayed to the right of the text.
However, if I remove android:layout_weight="1" from the TextView, the TextView now takes up the entire display width. The white square is no longer displayed.
- Why would layout_weight in the TextView affect whether or not the white square is displayed? Shouldn't the View with the white background always be assigned 32dpx32dp first? (It makes no difference if the view were any other types - ImageView or TextView).
- The problem I was working on is that I want the white square to always be displayed to the right of the TextView (whether or not the text is wrapped), but I don't want any empty space between the TextView and the white square. (If I add android:layout_weight="1" to the TextView, then there is a gap if the text is not wrapped.)
Any help would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
回答我的问题#1:通过查看 LinearLayout:layout_weight不仅为子级分配未使用的空间,如果子级超出边界,它还会用layout_weight缩小子级的线性布局。这解释了为什么带有换行文本的 TextView 在我的布局中缩小了。
至于我的问题#2的答案,我认为你的意思是 android:toRigthOf 而不是 android:layout_alignRight。使用RelativeLayout而不是LinearLayout不会改变布局行为。棘手的部分是将视图直接放置在 TextView 的右侧,没有间隙,无论文本是否换行。设置 maxWidth 会限制 TextView 的宽度,但该解决方案不会跨纵向/横向和不同的显示尺寸进行缩放。
解决方案 - 看起来 Dyarish 的解决方案是最好的。无论您使用哪种布局,我的布局问题都存在。关键是为 TextView 设置 maxWidth,以便它不会占用布局中的所有水平空间。由于在 TextView 中硬编码 android:maxWidth 值不会在不同的显示器上缩放,因此按照 Dyarish 的建议,在运行时设置 maxWidth 是一个很好的解决方案。
To answer my question #1: One thing I learned by looking at the source for LinearLayout: Not only does layout_weight assign unused space to a child, it also shrinks a child with layout_weight if the child extends beyond the bounds of the LinearLayout. That explains why a TextView with wrapped text is shrunk in my layout.
As for the answer to my question #2, I think you meant android:toRigthOf instead of android:layout_alignRight. Using a RelativeLayout instead of a LinearLayout doesn't change the layout behavior. The tricky part is placing a view immediately to the right of a TextView, without gaps, whether or not the text is wrapped. Setting a maxWidth would limit the TextView's width, but that solution doesn't scale across portrait/landscape and different display dimensions.
Solution - Looks like Dyarish's solution is the best available. My layout problem exists regardless of the layout you use. The key is to set a maxWidth for the TextView so that it doesn't take up the all of the horizontal space in the layout. Because hardcoding a android:maxWidth value in the TextView doesn't scale across different displays, setting the maxWidth at runtime, as Dyarish suggested, is a good solution.
希望这就是您正在寻找的。
首先,这是我为 创建 UI 找到的一个很棒的资源。
layout_weight - 指定布局中有多少额外空间要分配给视图。
如果要确保白色方块始终位于文本视图的右侧,可以使用相对视图,并将参数添加到视图中。 android:layout_alignRight="+id@yourTextViewID"。这应该始终使该框出现在 textView 区域的右侧。您可能还应该添加类似 android:maxWidth="250px" 的内容,这将确保您不会将白框完全推出屏幕。
这是一个代码示例:
您还可以添加到视图:
android:layout_alignTop="@+id/TextForWhiteBox" android:layout_alignBottom="@+id/TextForWhiteBox"
以使白框相同大小与 TextView 相同。
Hopefully this is what you are looking for.
First off, here is a great resource I found for Creating UI's.
layout_weight - Specifies how much of the extra space in the layout to be allocated to the View.
If you want to ensure that the white square is always to the right of the textview, you can use a Relative View, and add the parameter to the view. android:layout_alignRight="+id@yourTextViewID". This should always make the box appear right beside the textView area. You should probably also add something like android:maxWidth="250px" This will ensure that you don't push the white box completely out of the screen.
Here is a code sample:
You could also add to the View:
android:layout_alignTop="@+id/TextForWhiteBox" android:layout_alignBottom="@+id/TextForWhiteBox"
to make the white box the same size as the TextView.
首先,我测试了我的其他答案中的代码,它完全符合您所描述的要求。 (除非我误解了你的要求)。您绝对不想想要使用 android:layout_alignRight,它不是代码示例中的内容。这只会将框保留在屏幕的右侧,而根本不受文本视图的影响。此示例使用
android:layout_toRightOf="@+id/TextForWhiteBox"
这是可能的,因为它是相对布局。由于相对布局允许您相对于其他对象放置对象。该行始终将框放置在文本视图的右侧,没有间隙。至于屏幕方向的变化:
当方向变化时,它会创建一个新的视图实例。
这是一个简单的解决方案。
这是我使用的 main.xml:
您可能需要一些额外的逻辑来保留屏幕值。
该代码已经过测试,您应该能够直接复制并粘贴此代码以按照您的要求工作。
另外,根据您的逻辑,您可以使用类似的方法来返回屏幕方向。
Firstly I've tested the code from my other answer and it does exactly what you've described you've wanted. (unless I'm misunderstanding what you are asking for). You definitely do not want to use the android:layout_alignRight which is not what is in the code sample. That would simply keep the box on the right hand of the screen and not be affected by the textview at all. This sample uses
android:layout_toRightOf="@+id/TextForWhiteBox"
which is possible due to it being a relative layout. Since the Relative Layout allows you to place objects in relation to others. That line will always place the box just to the right of the textview with no gaps.As for the screen orientation changes:
When the orientation changes it creates a new instance of the view.
Here is a simple solution.
Here is the main.xml I used:
You might need some additional logic to keep screen values.
This code has been tested, you should be able to literally copy and paste this to work as you asked.
Also depending on your logic you could use something like this to return the screen orientation.