简体   繁体   English

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

[英]Android - TextView alignment for random text

I am developing an app. 我正在开发一个应用程序。 In that I am getting data from webservice and I am inserting it in TextView . 因为我从webservice获取数据,我将其插入TextView But, the alignment of the textview is getting scrambled. 但是,textview的对齐正在变得混乱。 Please check the image. 请检查图像。

First 3 rows(black colored) is in proper order, from the 4th it is scrambled. 前3行(黑色)按正确顺序排列,从4号开始加扰。 I googled on it so many times but not getting the exact solution as I wanted. 我搜索了很多次,但没有得到我想要的确切解决方案。

在此输入图像描述

XML Code: 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>

you might wanna assigh android:layout_weight property to the TextView s like: 你可能想把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" />

you will need to assign weight according to your requirement. 你需要根据你的要求分配重量 ie for bigger views you want to assign higher values. 即对于较大的视图,您希望分配更高的值。

[EDIT] and for the last TextView you may want to set android:layout_gravity="center|right" and android:layout_gravity="center|left" for the first for the text alignment for the rest you should use android:layout_gravity="center" [编辑]和最后一个TextView你可能想设置android:layout_gravity="center|right"android:layout_gravity="center|left"为第一个用于文本对齐的其余部分你应该使用android:layout_gravity="center"

Try this.. 尝试这个..

You forget to add android:layout_weight="1" for below two TextView Use android:singleLine="true" for all TextView 你忘了添加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"
    /> 

Add gravity to your TextView like: 将重力添加到TextView如:

android:gravity="center_vertical|center_horizontal"

change gravity to left, right or center as you need... 根据需要将重力改为左,右或中心......

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

It's a little more work, but did you try using TableLayout ? 这是一个更多的工作,但你尝试使用TableLayout This is very useful tutorial that can help you organizing the correct order of views. 是非常有用的教程,可以帮助您组织正确的视图顺序。

EDIT: I think i know what causes the mismatch of the TextViews . 编辑:我想我知道是什么导致TextViews不匹配。 It comes from the different length of the values in your last column named VENUE . 它来自您上一列名为VENUE的值的不同长度。 Take for example the values HYDEBARAD and DELHI - the first one is much longer than the second which makes all the TextViews on the row to go left. 就拿值HYDEBARAD德里 -第一个比第二要长得多,这使得所有的TextViews该行向左走。 I am sure that if you put identical padding to the TextViews you will align them. 我相信如果你在TextViews相同的padding ,你将对齐它们。 For example: take your TextViews in MATCH column and write the following: myTextView.setPadding(40, 0, 0, 0) ( padding on the left side) this will put them exactly in a straight row. 例如:在MATCH列中取出TextViews并编写以下内容: myTextView.setPadding(40, 0, 0, 0) (左侧padding )这将使它们完全成直线。 Do the same with the rest TextViews with the proper padding value, take into consideration the length of the values in the other columns... It is a little bit tricky though. 使用正确的padding值对其余的TextViews执行相同的操作,考虑其他列中的值的长度......但这有点棘手。 :) :)

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

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