【发布时间】:2012-03-17 08:36:22
【问题描述】:
我还是个 Android 菜鸟,如果这很简单,我只是没看到,请原谅我。
一个视图中有两部分文本水平跨越整个宽度,但只有一行文本那么高。左侧必须始终完整显示,但不应占用比所需更多的水平空间。右侧应被左侧推过并填满屏幕宽度的其余部分。如果右侧文本小于此宽度,则文本应水平右对齐。如果文本大于宽度,它应该水平滚动。
右侧的文本会经常更新,当应用告诉它时应该用新文本向上滑动(解释布局中的 TextSwitcher)。
我尝试了两种不同的布局样式。在这两种情况下,我都可以让左侧“推动”布局,让右侧滚动,但我不知道如何让右侧右对齐。它总是左对齐。这是一张显示正在发生的事情的图片......
http://img10.imageshack.us/img10/5599/androidlayout.png
此外(但不太重要),在我的布局代码中,我在 TextViews 上有 android:fadingEdge="none",但是当它滚动时,它的左侧和右侧仍然有一个褪色的边缘。这是为什么呢?
这是我创建的两个布局,它们产生了显示的结果,但不是我想要的结果。
使用水平线性布局...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayoutStatusBar"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="2px"
android:background="#555555"
>
<TextView
android:id="@+id/TextViewTimer"
android:textSize="18px"
android:textColor="#FFFFFF"
android:layout_gravity="left"
android:layout_weight="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0px"
android:layout_marginRight="3px"
android:text="Left Side"
>
</TextView>
<TextSwitcher
android:id="@+id/TextSwitcherDetails"
android:inAnimation="@anim/push_up_in"
android:outAnimation="@anim/push_up_out"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginLeft="3px"
android:layout_marginRight="0px"
>
<TextView
android:id="@+id/TextViewDetails1"
android:textSize="18px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:fadingEdge="none"
android:text="Right Side 1"
>
</TextView>
<TextView
android:id="@+id/TextViewDetails2"
android:textSize="18px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:fadingEdge="none"
android:text="Right Side 2 - This is a really long text this is long and fun and fun and long"
>
</TextView>
</TextSwitcher>
</LinearLayout>
那么如何让右侧的文本右对齐。谢谢!
编辑:
我发现了问题...有一些地方出了问题。首先,我发现了重力和 layout_gravity 之间的区别。我需要使用重力来右对齐文本。
我无法添加更多链接,所以复制并粘贴下面的链接
thinkandroid.wordpress.com/2010/01/14/how-to-position-views-properly-in-layouts/
但是,这并没有提供我想要的布局,因为长文本行在滚动之前会右对齐,并且消息的前部在循环播放之前无法阅读。我需要做的是使用视图的三个“列”,中间(空)视图的权重为“1”,而其他视图的权重为“0”,以便将最右边的文本尽可能推到右,同时仍然保持左对齐。
接下来,我发现一个TextSwitcher的宽度和最大的子TextView一样。当使用 setText() 从长行切换到短行时,这是一个问题,因为中间列不会将小文本推到边缘。解决方案是使用两个 setText()。一个导致淡出动画,然后在动画之后推一个runnable运行再次设置文本以导致新行的淡入动画。
现在我唯一的问题是淡出动画在长行文本上运行的时候,如果是在滚动中,它会重置到淡出前的X=0位置,所以效果是这样的它滚动,跳到原点位置,然后淡出。有人知道如何解决这个问题吗?
(我删除了上面的RelativeLayout代码,因为那是错误的,因为这篇文章很长。)
这是工作布局代码...
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayoutStatusBar"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2px"
android:background="#333333"
>
<TextView
android:id="@+id/TextViewTimer"
android:textSize="18px"
android:textColor="#FFFFFF"
android:layout_gravity="top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_marginLeft="1px"
android:layout_marginRight="4px"
android:text="Left Side"
>
</TextView>
<View
android:id="@+id/ViewSpacer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
></View>
<TextSwitcher
android:id="@+id/TextSwitcherDetails"
android:inAnimation="@anim/push_up_in"
android:outAnimation="@anim/push_up_out"
android:layout_gravity="top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_marginRight="1px"
>
<TextView
android:id="@+id/TextViewDetails1"
android:textSize="18px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:fadingEdge="none"
>
</TextView>
<TextView
android:id="@+id/TextViewDetails2"
android:textSize="18px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:fadingEdge="none"
>
</TextView>
</TextSwitcher>
</LinearLayout>
【问题讨论】:
-
关于如何在布局的最右侧放置视图时,我遇到了类似的问题。您关于实施三个“列”的建议非常正确!