繁体   English   中英

Android - 随机文本的TextView对齐方式

[英]Android - TextView alignment for random text

我正在开发一个应用程序。 因为我从webservice获取数据,我将其插入TextView 但是,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>

你可能想把android:layout_weight属性告诉TextView如:

<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" />

你需要根据你的要求分配重量 即对于较大的视图,您希望分配更高的值。

[编辑]和最后一个TextView你可能想设置android:layout_gravity="center|right"android:layout_gravity="center|left"为第一个用于文本对齐的其余部分你应该使用android:layout_gravity="center"

尝试这个..

你忘了添加android:layout_weight="1"为空两TextView使用android:singleLine="true"为所有TextView

<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"
    /> 

将重力添加到TextView如:

android:gravity="center_vertical|center_horizontal"

根据需要将重力改为左,右或中心......

你有没有尝试为schedule_1_textView_venue添加权重?

这是一个更多的工作,但你尝试使用TableLayout 是非常有用的教程,可以帮助您组织正确的视图顺序。

编辑:我想我知道是什么导致TextViews不匹配。 它来自您上一列名为VENUE的值的不同长度。 就拿值HYDEBARAD德里 -第一个比第二要长得多,这使得所有的TextViews该行向左走。 我相信如果你在TextViews相同的padding ,你将对齐它们。 例如:在MATCH列中取出TextViews并编写以下内容: myTextView.setPadding(40, 0, 0, 0) (左侧padding )这将使它们完全成直线。 使用正确的padding值对其余的TextViews执行相同的操作,考虑其他列中的值的长度......但这有点棘手。 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM