简体   繁体   English

Android以编程方式将文本视图添加到相对布局

[英]android programatically adding text views to a relative layout

hi im trying to add text fields based on a JSON Feed i have a view that displays a result but i'm debating with myself should i create a text field in that layout and programmatically tell the text field how many lines there are and add line breaks or do i add a new text field to the view for each item in the feed. 嗨,我尝试添加基于JSON Feed的文本字段,我有一个显示结果的视图,但我正在与我自己争论是否应该在该布局中创建一个文本字段并以编程方式告诉该文本字段有多少行并添加行中断还是我要为提要中的每个项目在视图中添加新的文本字段。

heres what i want to acheive 这是我想达到的目标

info                     info
Team Name     1-3   Team Name
Scorer                 Scorer
                       Scorer
                       Scorer

heres my layout so far it has everything but the scorers 到目前为止,这是我的布局,除了得分手之外,它拥有一切

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:paddingBottom="30dp"
    android:paddingTop="10dp" >

    <TextView
        android:id="@+id/Division"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="5dp"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/HomeTeam"
        android:layout_width="115dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/Division"
        android:layout_below="@+id/Division"
        android:text="Medium Text"
     android:textSize="20dp"
        android:textColor="@color/blue" 
        android:ellipsize="end"
        android:lines="2"
        android:gravity="left"
        android:textStyle="bold"
        />

    <TextView
        android:id="@+id/Attendence"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/HomeTeam"
        android:layout_alignParentRight="true"
        android:layout_marginRight="18dp"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/AwayTeam"
        android:layout_width="115dp"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/Attendence"
        android:layout_below="@+id/Attendence"
        android:text="Medium Text"
     android:textSize="20dp"
        android:textColor="@color/blue"
        android:ellipsize="end"
        android:lines="2"
        android:gravity="right"
        android:textStyle="bold"
         />

    <TextView
        android:id="@+id/TextView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/Attendence"
        android:layout_centerHorizontal="true"
        android:text="-"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/blue" />

    <TextView
        android:id="@+id/AwayScore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/TextView02"
        android:layout_alignBottom="@+id/TextView02"
        android:layout_toRightOf="@+id/TextView02"
        android:text="0"
         android:textSize="20dp"
        android:textColor="@color/blue"
        android:layout_marginLeft="10dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/HomeScore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/TextView02"
        android:layout_alignBottom="@+id/TextView02"
        android:layout_toLeftOf="@+id/TextView02"
        android:text="0"
        android:textSize="20dp"
        android:textColor="@color/blue" 
        android:layout_marginRight="10dp"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/HomeScorers"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/HomeTeam"
        android:layout_below="@+id/HomeTeam"

        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/AwayScorers"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/AwayTeam"
        android:layout_below="@+id/AwayTeam"

        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

Please Check out the link. 请检查链接。 Here Create relative layout Programatically. 在此处以编程方式创建相对布局。 I think it Help you. 我认为它可以帮助您。

Link 链接

thanks 谢谢

  • create a xml of a particular row (as create in list) Add a empty 创建特定行的xml(如在列表中创建)添加空
  • List item linear layout/Table layout in Layout xml. 在布局xml中列出项目线性布局/表布局。
  • Inflate that row in java file as per json data in loop view 在循环视图中根据json数据在Java文件中填充该行
  • set the data as sets in list get function. 将数据设置为列表获取功能中的设置。

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

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