【问题标题】:Align text to right or to bottom depending on space根据空间将文本右对齐或下对齐
【发布时间】:2016-03-03 17:21:24
【问题描述】:

我有两个文本视图,我想根据空间将第二个文本视图对齐到第一个文本视图的右侧或底部。结果应该是这样的:

第一种情况(单行):

Text1 一行 Text2

第二种情况(多行):

Text1 示例与

两行
___________文本2

如您所见,在第一种情况下,第二个 textview 向右对齐,而在第二种情况下,它向右和底部对齐。我怎样才能在 xml 中实现该结果?

提前致谢!

【问题讨论】:

  • 什么意思,Text1 比屏幕大?

标签: android android-layout android-view android-layoutparams


【解决方案1】:

您可以使用FlowLayout ViewGroup 作为父级。

https://github.com/ApmeM/android-flowlayout

【讨论】:

    【解决方案2】:

    你是什么意思 Text1 比屏幕大?

    这是我写的示例代码:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:background="@android:color/holo_green_dark"
        android:layout_height="wrap_content">
    
        <TextView
        android:layout_weight="1"
        android:gravity="bottom|left"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="your text more than 1 line"/>
    
        <TextView
            android:gravity="center|bottom"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Text2"/>
    
    </LinearLayout>
    

    为了让Text1在左边,Text2在右边,使用LinearLayout并设置orientation:"horizontal"

    如果这不能满足您的要求,请告诉我。

    与你的问题有关,你只需要玩TextViewgravity,如果Textview的值是动态的,你必须在你的java文件中创建条件。

    【讨论】:

    • 感谢您的回答,这不是我想要的。让我解释一下,如果第一个文本没有填满屏幕的宽度,我希望第二个文本视图与第一个文本视图的右侧对齐。并且,如果第一个 textview 中有不止一行,则在右下角。
    • 检查我更新的答案。我在底部添加了更多的 1 行和文本 2。如果还没有您需要的东西,请告诉我。
    • 谢谢,看看我的例子。多行时,我需要 text2 位于右下角,而不仅仅是 text1 的右侧 :(
    • 你在你的项目中尝试过我的代码吗?如果您需要TextView2 是正确的,您可以将TextView2android:gravity 更改为right|bottom 而不是center|bottom,或者如果您需要在java 中实现,您只需创建Textview2 的条件gravity
    猜你喜欢
    • 2011-12-16
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2021-02-21
    • 2012-04-06
    • 2012-02-24
    • 2019-04-20
    • 1970-01-01
    相关资源
    最近更新 更多