Android文本布局问题:两个文本视图,并排,具有不同的布局对齐和权重

Con*_*ure 7 android android-layout

我仍然是一个Android noob,原谅我,如果这很简单,我只是没有看到它.

视图中有两个文本部分水平跨越整个宽度,但只有一行文本高.左侧必须始终显示完整,但不应占用超出其需要的水平空间.应将左侧推过右侧并填满屏幕宽度的其余部分.如果右侧文本小于此宽度,则文本应水平右对齐.如果文本大于宽度,则应水平滚动.

右侧的文本将经常更新,并应在应用程序告诉它时向上滑动新文本(解释布局中的TextSwitcher).

我尝试了两种不同的布局样式.在这两种情况下,我可以让左侧"推"布局,右侧滚动,但我无法弄清楚如何让右侧对齐.它始终保持对齐.这是一张显示正在发生的事情的图片......

http://img10.imageshack.us/img10/5599/androidlayout.png

另外(但不太重要),在我的布局代码中,我在TextViews上有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>
Run Code Online (Sandbox Code Playgroud)

那么如何让右侧的文本右对齐.谢谢!


编辑:

我发现了问题......有一些问题.首先,我发现了gravity和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>
Run Code Online (Sandbox Code Playgroud)

Con*_*ure 4

我发现问题了!有一些事情是错误的。首先,我发现了gravity和layout_gravity之间的区别。我需要使用重力来右对齐文本。

http://thinkandroid.wordpress.com/2010/01/14/how-to-position-views-properly-in-layouts/

然而,这并没有给我想要的布局,因为长行文本在滚动之前将右对齐,并且消息的前部在循环之前无法读取。我需要做的是使用三“列”视图,中间(空)视图的权重为“1”,而其他视图的权重为“0”,以便将最右边的文本尽可能地推到最右边的文本。右对齐,同时仍保持左对齐。

接下来,我发现 TextSwitcher 的宽度与最大的子 TextView 的宽度相同。当使用 setText() 从长行切换到短行时,这是一个问题,因为中间列不会将小文本推到边缘。解决方案是使用两个 setText()。一种是引起淡出动画,然后推送一个可运行对象在动画之后运行以再次设置文本以引起新行的淡入动画。

不幸的是,当文本每五到十秒切换一次时,水平滚动文本看起来很糟糕。所以我只是让它从屏幕上消失。