本文介绍了Android的文字排版的问题:二textviews,并排侧,具有不同的布局路线和权重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还是有点的Andr​​oid小白的,原谅我,如果这很简单,我只是没有看到它。

有在水平方向跨越整个宽度,但仅高达一行文本的视图文本的两部分。左侧必须始终显示为满,但比它需要应该没有更多的横向空间。右侧应由左侧被推倒,并填补了屏幕宽度的其余部分。如果右侧文本比这个宽度,文字应该是右对齐的水平。如果文本大于宽度,应该横向滚动。

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

我已经尝试了两种不同的布局样式。在这两种情况下,我能得到左边为推的布局,右侧滚动,但我无法弄清楚如何获得右侧右对齐。它始终是左对齐。下面是一个画面显示发生了什么......

另外的(但不那么重要了),在我的布局code我有安卓fadingEdge =上TextViews无,但它仍然有在左,右侧边褪色时滚动。这是为什么?

下面是两个布局我创建的,它产生所示的结果,而不是结果我想要的。

使用一个水平的LinearLayout ...

 <?XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / LinearLayoutStatusBar
    机器人:方向=横向
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_margin =2px的
    机器人:背景=#555555
>
    <的TextView
        机器人:ID =@ + ID / TextViewTimer
        机器人:TEXTSIZE =18像素
        机器人:文字颜色=#FFFFFF
        机器人:layout_gravity =左
        机器人:layout_weight =0
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginLeft =0像素
        机器人:layout_marginRight =的3px
        机器人:文字=左侧
    >
    < / TextView的>
    < TextSwitcher
        机器人:ID =@ + ID / TextSwitcherDetails
        机器人:inAnimation =@动画/ push_up_in
        机器人:outAnimation =@动画/ push_up_out
        机器人:layout_weight =1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_gravity =右
        机器人:layout_marginLeft =的3px
        机器人:layout_marginRight =0像素
    >
        <的TextView
            机器人:ID =@ + ID / TextViewDetails1
            机器人:TEXTSIZE =18像素
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:layout_gravity =右
            机器人:单线=真
            机器人:ellipsize =金字招牌
            机器人:marqueeRepeatLimit =marquee_forever
            机器人:scrollHorizo​​ntally =真
            机器人:可聚焦=真
            机器人:focusableInTouchMode =真
            机器人:fadingEdge =无
            机器人:文字=右侧1
        >
        < / TextView的>
        <的TextView
            机器人:ID =@ + ID / TextViewDetails2
            机器人:TEXTSIZE =18像素
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:layout_gravity =右
            机器人:单线=真
            机器人:ellipsize =金字招牌
            机器人:marqueeRepeatLimit =marquee_forever
            机器人:scrollHorizo​​ntally =真
            机器人:可聚焦=真
            机器人:focusableInTouchMode =真
            机器人:fadingEdge =无
            机器人:文字=右侧2 - 这是一个很长的文字,这是漫长而有趣和乐趣和长
        >
        < / TextView的>
    < / TextSwitcher>
< / LinearLayout中>

那么,如何获得右侧为右对齐文本。谢谢!


编辑:

我发现这个问题......有几件事情是错误的。首先,我发现了万有引力和layout_gravity之间的差异。我需要利用重力右对齐文本。

我不能添加任何更多的链接,所以复制并粘贴以下

链接

thinkandroid.word press.com / 2010/01/14 /如何对位置的意见,妥善式的布局/

滚动和无法读取该消息的前部,直到其周围循环前

不过,这并没有给我正是我想要的布局,因为文字的长行会右对齐。我需要做的是使用的意见三个列,以在中间(空)视图中的权重为1,而其他的0,使之尽可能推最右侧的文本到右,同时仍保持左对齐。

接下来,我发现了一种TextSwitcher的宽度是相同的最大子TextView的。使用的setText(),因为中间一列不超给力推动小文从长行切换到短线时,这是一个问题。解决的办法是使用两个的setText()秒。之一,以使淡出的动画,然后推可运行动画后运行再次设置文本以使淡入新线的动画。

现在我唯一的问题是,当淡出动画文本长线运行,如果是在中滚动,其重置为X = 0位置褪色之前,这样的效果是它滚动,跳转到初始位置,然后淡出。任何人都知道如何解决这个问题?

(我删除了RelativeLayout的code以上,因为这是错误的,因为这个职位是很长的。)

下面是工作布局code ...

 <的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / LinearLayoutStatusBar
    机器人:方向=横向
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_margin =2px的
    机器人:背景=#333333
>
    <的TextView
        机器人:ID =@ + ID / TextViewTimer
        机器人:TEXTSIZE =18像素
        机器人:文字颜色=#FFFFFF
        机器人:layout_gravity =顶
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =0
        机器人:layout_marginLeft =1像素
        机器人:layout_marginRight =递四方
        机器人:文字=左侧
    >
    < / TextView的>
    <查看
        机器人:ID =@ + ID / ViewSpacer
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =1
    >< /视图>
    < TextSwitcher
        机器人:ID =@ + ID / TextSwitcherDetails
        机器人:inAnimation =@动画/ push_up_in
        机器人:outAnimation =@动画/ push_up_out
        机器人:layout_gravity =顶
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =0
        机器人:layout_marginRight =1像素
    >
        <的TextView
            机器人:ID =@ + ID / TextViewDetails1
            机器人:TEXTSIZE =18像素
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:单线=真
            机器人:ellipsize =金字招牌
            机器人:marqueeRepeatLimit =marquee_forever
            机器人:scrollHorizo​​ntally =真
            机器人:可聚焦=真
            机器人:focusableInTouchMode =真
            机器人:fadingEdge =无
        >
        < / TextView的>
        <的TextView
            机器人:ID =@ + ID / TextViewDetails2
            机器人:TEXTSIZE =18像素
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:单线=真
            机器人:ellipsize =金字招牌
            机器人:marqueeRepeatLimit =marquee_forever
            机器人:scrollHorizo​​ntally =真
            机器人:可聚焦=真
            机器人:focusableInTouchMode =真
            机器人:fadingEdge =无
        >
        < / TextView的>
    < / TextSwitcher>
< / LinearLayout中>


解决方案

我发现这个问题!有几件事情是错误的。首先,我发现了万有引力和layout_gravity之间的差异。我需要利用重力右对齐文本。

滚动和无法读取该消息的前部,直到其周围循环前

不过,这并没有给我正是我想要的布局,因为文字的长行会右对齐。我需要做的是使用的意见三个列,以在中间(空)视图中的权重为1,而其他的0,使之尽可能推最右侧的文本到右,同时仍保持左对齐。

接下来,我发现了一种TextSwitcher的宽度是相同的最大子TextView的。使用的setText(),因为中间一列不超给力推动小文从长行切换到短线时,这是一个问题。解决的办法是使用两个的setText()秒。之一,以使淡出的动画,然后推可运行动画后运行再次设置文本以使淡入新线的动画。

不幸的是,水平滚动文本时,文本每五到十秒切换看起来很可怕。所以我只是让它跑出画面。

I'm still a bit of an Android noob, forgive me if this is simple and I'm just not seeing it.

There are two portions of text in a view that spans the entire width horizontally, but is only as high as one line of text. The left side must always be displayed in full, but should take no more horizontal space than it needs. The right side should be pushed over by the left side and fill up the remainder of the screen width. If the right side text is smaller than this width, the text should be right-aligned horizontally. If the text is greater than the width, it should scroll horizontally.

The text on the right side will be updated frequently and should slide up with new text when the app tells it (explaining the TextSwitcher in the layout).

I have tried two different layout styles. In both situations, I can get the left side to "push" the layout, the right side to scroll, but I can't figure out how to get the right side to right align. It is always left aligned. Here is a picture showing what is happening...

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

In addition (but less important), in my layout code I have android:fadingEdge="none" on the TextViews, but it still has a faded edge on the left and right side when it scrolls. Why is that?

Here are the two layouts I created, which yield the results shown, but not the results I want.

Using a horizontal 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>

So how do I get that text on the right side to right-align. Thanks!


EDIT:

I found the problem... There were a few things wrong. First, I discovered the difference between gravity and layout_gravity. I needed to use gravity to right-align the text.

I can't add any more links, so copy and paste the link below

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

However, that didn't give me exactly the layout I wanted, because long lines of text will be right-aligned before scrolling and the front part of the message cannot be read until it loops around. What I needed to do was use three "columns" of views, with the weight on the middle (empty) view to be "1" while the others "0" so that it pushes the right-most text as far as possible to the right, while still maintaining left-alignment.

Next, I discovered that the width of a TextSwitcher is the same as the largest child TextView. This is a problem when switching from a long line to a short line using setText() because the middle column won't push the small text over to the edge. The solution is to use two setText()s. One to cause the fade-out animation, then push a runnable to run after the animation to set the text again to cause the fade-in animation of the new line.

Now my only problem is that when the fade-out animation runs on a long line of text, if it is in mid-scroll, it resets to the X=0 position before fading, so the effect is that it scrolls, jumps to the origin position, then fades out. Anybody know how to fix that?

(I removed the RelativeLayout code above since that was wrong, since this post is really long.)

Here is the working layout code...

<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>
解决方案

I found the problem! There were a few things wrong. First, I discovered the difference between gravity and layout_gravity. I needed to use gravity to right-align the text.

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

However, that didn't give me exactly the layout I wanted, because long lines of text will be right-aligned before scrolling and the front part of the message cannot be read until it loops around. What I needed to do was use three "columns" of views, with the weight on the middle (empty) view to be "1" while the others "0" so that it pushes the right-most text as far as possible to the right, while still maintaining left-alignment.

Next, I discovered that the width of a TextSwitcher is the same as the largest child TextView. This is a problem when switching from a long line to a short line using setText() because the middle column won't push the small text over to the edge. The solution is to use two setText()s. One to cause the fade-out animation, then push a runnable to run after the animation to set the text again to cause the fade-in animation of the new line.

Unfortunately, horizontally scrolling the text looks terrible when the text switches every five to ten seconds. So I'm just letting it run off the screen.

这篇关于Android的文字排版的问题:二textviews,并排侧,具有不同的布局路线和权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 07:08