我还是一个 Android 菜鸟,请原谅我,如果这很简单,但我只是没有看到它。
视图中有两部分文本水平跨越整个宽度,但只有一行文本那么高。左侧必须始终完整显示,但不应占用超过其所需的水平空间。右侧应被左侧推过,并填满屏幕宽度的剩余部分。如果右侧文本小于此宽度,则文本应水平右对齐。如果文本大于宽度,则应水平滚动。
右侧的文本将经常更新,并且当应用程序告诉它时(解释布局中的 TextSwitcher),应该用新文本向上滑动。
我尝试过两种不同的布局风格。在这两种情况下,我都可以让左侧“推动”布局,让右侧滚动,但我不知道如何让右侧右对齐。它始终保持左对齐。这是一张显示正在发生的事情的图片......
http://img10.imageshack.us/img10/5599/androidlayout.png http://img10.imageshack.us/img10/5599/androidlayout.png
另外(但不太重要),在我的布局代码中,我在 TextView 上有 android:fadingEdge="none" ,但是当它滚动时,它的左侧和右侧仍然有一个褪色的边缘。这是为什么?
这是我创建的两个布局,它们产生了所示的结果,但不是我想要的结果。
使用水平 LinearLayout...
<?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>
那么如何让右侧的文本右对齐呢?谢谢!
EDIT:
我发现了问题...有一些问题。首先,我发现了gravity和layout_gravity之间的区别。我需要使用重力来右对齐文本。
我无法添加更多链接,因此请复制并粘贴下面的链接
thinkandroid.wordpress.com/2010/01/14/how-to-position-views-properly-in-layouts/
然而,这并没有给我想要的布局,因为长行文本在滚动之前将右对齐,并且消息的前部在循环之前无法读取。我需要做的是使用三“列”视图,中间(空)视图的权重为“1”,而其他视图的权重为“0”,以便将最右边的文本尽可能地推到最右边的文本。右对齐,同时仍保持左对齐。
接下来,我发现 TextSwitcher 的宽度与最大的子 TextView 的宽度相同。当使用 setText() 从长行切换到短行时,这是一个问题,因为中间列不会将小文本推到边缘。解决方案是使用两个 setText()。一种是引起淡出动画,然后推送一个可运行对象在动画之后运行以再次设置文本以引起新行的淡入动画。
现在我唯一的问题是,当淡出动画在一长行文本上运行时,如果处于滚动中间,它会重置到淡出之前的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>