【问题标题】:Android - TextView alignment for random textAndroid - 随机文本的 TextView 对齐
【发布时间】:2014-04-01 11:44:14
【问题描述】:

我正在开发一个应用程序。我正在从 web 服务获取数据,并将其插入到 TextView 中。但是,文本视图的对齐方式变得混乱。请检查图像。

前 3 行(黑色)按正确顺序排列,从第 4 行开始被打乱。我用谷歌搜索了很多次,但没有得到我想要的确切解决方案。

XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
>

     <TextView
        android:id="@+id/schedule_1_textView_day_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="KKR"
        android:layout_weight="1.5"
        android:layout_marginTop="10dp"
        android:textColor="@color/Black"            
        android:textSize="7sp"
        />        

    <TextView
        android:id="@+id/schedule_1_textView_matchno"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="10"
        android:layout_weight="1.5"
        android:layout_marginTop="10dp"
        android:textColor="@color/Black"
        android:textSize="7sp"
        />        

    <TextView
        android:id="@+id/schedule_1_textView_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:text="6"
        android:textColor="@color/Black"
        android:textSize="7sp"
        />   

    <TextView
        android:id="@+id/schedule_1_textView_team1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:text="4"
        android:textSize="7sp"
        android:textColor="@color/Black"
        />       

    <TextView
        android:id="@+id/schedule_1_textView_team2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:layout_weight="1"
        android:layout_marginTop="10dp"          
        android:textColor="@color/Black"
        android:textSize="7sp"
        />       

    <TextView
        android:id="@+id/schedule_1_textView_venue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="6"
        android:layout_marginTop="10dp"
        android:textColor="@color/Black"
        android:textSize="7sp"
        /> 

    <TextView
        android:id="@+id/schedule_1_textView_blank"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="B"
        android:layout_marginTop="20dp"
        android:textColor="@color/Black"
        android:textSize="6sp"
        android:visibility="invisible"
        /> 


 </LinearLayout>

【问题讨论】:

  • 将任何地方的layout_width设置为"0"

标签: android textview text-justify


【解决方案1】:

试试这个..

您忘记为以下两个TextView 添加android:layout_weight="1" 为所有TextView 使用android:singleLine="true"

<TextView
    android:id="@+id/schedule_1_textView_venue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:singleLine="true"
    android:text="6"
    android:layout_marginTop="10dp"
    android:textColor="@color/Black"
    android:textSize="7sp"
    /> 

<TextView
    android:id="@+id/schedule_1_textView_blank"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:singleLine="true"
    android:text="B"
    android:layout_marginTop="20dp"
    android:textColor="@color/Black"
    android:textSize="6sp"
    android:visibility="invisible"
    /> 

【讨论】:

    【解决方案2】:

    您可能想将 android:layout_weight 属性分配给 TextViews,例如:

    <TextView
        android:id="@+id/schedule_1_textView_venue"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="6"
        android:layout_marginTop="10dp"
        android:textColor="@color/Black"
        android:textSize="7sp" />
    

    您需要根据自己的要求分配权重。即对于更大的视图,您希望分配更高的值。

    [EDIT],对于最后一个 TextView,您可能需要为第一个设置 android:layout_gravity="center|right" 和 android:layout_gravity="center|left",以便为其余的 文本对齐应该使用android:layout_gravity="center"

    【讨论】:

    • 这可能是因为您的文本包含多余的空格。你可以通过使用.trim() 方法来解决这个问题,删除文本开头和结尾的多余空格。
    【解决方案3】:

    为您的TextView 添加重力,例如:

    android:gravity="center_vertical|center_horizontal"
    

    根据需要将重力更改为向左、向右或居中...

    【讨论】:

    • 我觉得您正在为您的数据获得空间,最好在添加到 TextView 之前尝试修剪它们...
    【解决方案4】:

    您是否尝试过为 schedule_1_textView_venue 增加权重?

    【讨论】:

    • 是的。但还是同样的问题
    • 你应该尝试使用 TableLayout 而不是 LinearLayout 然后列应该对齐你的文本视图
    【解决方案5】:

    还有一点工作要做,但是您尝试过使用TableLayout 吗? This 是非常有用的教程,可以帮助您组织正确的视图顺序。

    编辑:我想我知道导致TextViews 不匹配的原因。它来自您最后一列名为 VENUE 中值的不同长度。以值 HYDEBARAD 和 DELHI 为例 - 第一个比第二个长得多,这使得行上的所有 TextViews 向左移动。我敢肯定,如果您将相同的padding 放入TextViews,您将对齐它们。例如:在 MATCH 列中输入您的TextViews 并写入以下内容:myTextView.setPadding(40, 0, 0, 0)(左侧为padding)这会将它们完全排成一行。使用正确的padding 值对其余的TextViews 执行相同的操作,并考虑其他列中值的长度......不过这有点棘手。 :)

    【讨论】:

    • @Mitesh 你试过我的解决方案了吗? (:
    猜你喜欢
    • 2021-05-01
    • 1970-01-01
    • 2017-11-06
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    相关资源
    最近更新 更多