【问题标题】:Align horizontally TextViews which can contain multiple lines in Android在Android中水平对齐可以包含多行的TextView
【发布时间】:2017-06-25 05:37:24
【问题描述】:

我的问题如下。我有 4 个水平对齐的 TextView。这4个textview中,前2个只能占1行,而后2个可以占1或2行。但是我希望这样,当我转到第二行时,这将在左侧的第一个 textView 下继续(好像它都是唯一的 textview),而不是在 textview 开始的位置下方。例如,想想 facebook 帖子,其中第一个 textview 是用户名,第二个是他执行的操作,如果操作太长,那么文本将在用户名下继续。 现在我的代码如下:

<LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true">

            <TextView
                android:id="@+id/status_display_name"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:textColor="?android:textColorPrimary"
                android:textStyle="normal|bold"
                android:ellipsize="end"
                android:maxLines="1"
                android:text="Name "
                 />

            <TextView
                android:id="@+id/status_display_family_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="?android:textColorPrimary"
                android:maxLines="1"
                android:ellipsize="end"
                android:text="Family name"
                android:paddingRight="@dimen/status_display_name_right_padding"/>
            <TextView
                android:id="@+id/status_display_action"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="?android:textColorSecondary"
                android:maxLines="2"
                android:ellipsize="end"
                android:text="has done " />
            <TextView
                android:id="@+id/status_display_item_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="?android:textColorPrimary"
                android:maxLines="2"
                android:ellipsize="end"
                android:text="My item title" />
        </LinearLayout>

【问题讨论】:

    标签: android android-layout textview


    【解决方案1】:

    我可以建议您的问题的最佳解决方案是使用一 (1) 个而不是四 (4) 个文本视图。附加四个文本视图的所有文本并将其设置为单个文本视图,如下所示:

     yourTextView.setText(displayName + " " + familyName + " " + displayAction + " " + displayItemTitle);
    

    你的布局会是这样的:

    <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true">
    
            <TextView
                android:id="@+id/your_single_textview"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:textColor="?android:textColorPrimary"
                android:textStyle="normal|bold"
                android:text="Name "
                 />
    
        </LinearLayout>
    

    如果您想将某些文本设置为粗体但某些文本正常,那么您可以实现这一点。检查此链接:

    how to make a specific text on TextView BOLD

    【讨论】:

    • 但是我想当用户点击项目或名称时无法区分,所以它不是我想要的......
    • 好吧,其实应该可以stackoverflow.com/questions/10696986/…
    • 好像是这样,试试看。我会寻找更多选择,并会尽快回复您。
    猜你喜欢
    • 2016-05-13
    • 2019-12-02
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    相关资源
    最近更新 更多