2012-06-10 24 views
0

我遇到了相对布局有点问题。我正在做一个项目,我必须从.CSV文件中读取一些值,并在相对布局中动态显示它们。我会放几个代码片段和图片,然后解释我的问题。向android相对布局动态添加元素

第一个代码段:

package ekalavya.pratnala.quiz; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.util.StringTokenizer; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 
import android.widget.ScrollView; 

public class QuizActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    // Beginning of variable declarations 
    File quizSpecs = new File("mnt/sdcard/teacher.csv"); // Read the file 
    BufferedReader csvReader = null; 
    String line = ""; // Storing each line in a string 
    StringTokenizer currentLine = null; 
    int noOfQuestions = 0; // Number of questions in the quiz 
    int time = 0; // Duration of the quiz 
    int[][] quizData; // Storing the quiz specifications in an integer array 
    int i = 0, j = 0; // Loop variables 
    int[][] questionImages = { 
      { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, 
        R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h, 
        R.drawable.i, R.drawable.j }, 
      { R.drawable.a_checked, R.drawable.b_checked, 
        R.drawable.c_checked, R.drawable.d_checked, 
        R.drawable.e_checked, R.drawable.f_checked, 
        R.drawable.g_checked, R.drawable.h_checked, 
        R.drawable.i_checked, R.drawable.j_checked }, 
      { R.drawable.zero, R.drawable.one, R.drawable.two, 
        R.drawable.three, R.drawable.four, R.drawable.five, 
        R.drawable.six, R.drawable.seven, R.drawable.eight, 
        R.drawable.nine }, 
      { R.drawable.zero_checked, R.drawable.one_checked, 
        R.drawable.two_checked, R.drawable.three_checked, 
        R.drawable.four_checked, R.drawable.five_checked, 
        R.drawable.six_checked, R.drawable.seven_checked, 
        R.drawable.eight_checked, R.drawable.nine_checked } }; 
    // End of variable declarations 

    try { 
     csvReader = new BufferedReader(new FileReader(quizSpecs)); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    try { 
     line = csvReader.readLine(); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    currentLine = new StringTokenizer(line, ","); 
    noOfQuestions = Integer.parseInt(currentLine.nextToken()); 
    time = Integer.parseInt(currentLine.nextToken()); 
    while (currentLine.hasMoreTokens()) 
     ; 
    quizData = new int[noOfQuestions][6]; 
    for (i = 0; i < noOfQuestions; i++) { 
     try { 
      line = csvReader.readLine(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     currentLine = new StringTokenizer(line, ","); 
     for (j = 0; j < 6; j++) { 
      quizData[i][j] = Integer.parseInt(currentLine.nextToken()); 
     } 
    } 
    try { 
     csvReader.close(); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    ScrollView s1 = new ScrollView(this); 
    RelativeLayout r1 = new RelativeLayout(this); 
    for (i = 0; i < 2; i++) { 
     switch (quizData[i][1]) { 
     case 1: 
     case 2: 
      for (j = 0; j < quizData[i][2]; j++) { 
       ImageView option = new ImageView(this); 
       option.setImageResource(questionImages[0][j]); 
       option.setId(j + (10 * (i + 1))); 
       RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
         RelativeLayout.LayoutParams.WRAP_CONTENT, 
         RelativeLayout.LayoutParams.WRAP_CONTENT); 
       params.addRule(RelativeLayout.RIGHT_OF, j - 1 
         + (10 * (i + 1))); 
       option.setLayoutParams(params); 
       r1.addView(option, params); 
      } 
      break; 
     } 
    } 
    s1.addView(r1, new LayoutParams(LayoutParams.FILL_PARENT, 
      LayoutParams.FILL_PARENT)); 
    this.setContentView(s1); 
} 
} 

图片1:https://www.dropbox.com/s/vzpilyotvgtipbb/pic2.png

第二代码片段:

package ekalavya.pratnala.quiz; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.util.StringTokenizer; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 
import android.widget.ScrollView; 

public class QuizActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    // Beginning of variable declarations 
    File quizSpecs = new File("mnt/sdcard/teacher.csv"); // Read the file 
    BufferedReader csvReader = null; 
    String line = ""; // Storing each line in a string 
    StringTokenizer currentLine = null; 
    int noOfQuestions = 0; // Number of questions in the quiz 
    int time = 0; // Duration of the quiz 
    int[][] quizData; // Storing the quiz specifications in an integer array 
    int i = 0, j = 0; // Loop variables 
    int[][] questionImages = { 
      { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, 
        R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h, 
        R.drawable.i, R.drawable.j }, 
      { R.drawable.a_checked, R.drawable.b_checked, 
        R.drawable.c_checked, R.drawable.d_checked, 
        R.drawable.e_checked, R.drawable.f_checked, 
        R.drawable.g_checked, R.drawable.h_checked, 
        R.drawable.i_checked, R.drawable.j_checked }, 
      { R.drawable.zero, R.drawable.one, R.drawable.two, 
        R.drawable.three, R.drawable.four, R.drawable.five, 
        R.drawable.six, R.drawable.seven, R.drawable.eight, 
        R.drawable.nine }, 
      { R.drawable.zero_checked, R.drawable.one_checked, 
        R.drawable.two_checked, R.drawable.three_checked, 
        R.drawable.four_checked, R.drawable.five_checked, 
        R.drawable.six_checked, R.drawable.seven_checked, 
        R.drawable.eight_checked, R.drawable.nine_checked } }; 
    // End of variable declarations 

    try { 
     csvReader = new BufferedReader(new FileReader(quizSpecs)); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    try { 
     line = csvReader.readLine(); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    currentLine = new StringTokenizer(line, ","); 
    noOfQuestions = Integer.parseInt(currentLine.nextToken()); 
    time = Integer.parseInt(currentLine.nextToken()); 
    while (currentLine.hasMoreTokens()) 
     ; 
    quizData = new int[noOfQuestions][6]; 
    for (i = 0; i < noOfQuestions; i++) { 
     try { 
      line = csvReader.readLine(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     currentLine = new StringTokenizer(line, ","); 
     for (j = 0; j < 6; j++) { 
      quizData[i][j] = Integer.parseInt(currentLine.nextToken()); 
     } 
    } 
    try { 
     csvReader.close(); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    ScrollView s1 = new ScrollView(this); 
    RelativeLayout r1 = new RelativeLayout(this); 
    for (i = 0; i < 1; i++) { 
     switch (quizData[i][3]) { 
     case 1: 
     case 2: 
      for (j = 0; j < quizData[i][2]; j++) { 
       ImageView option = new ImageView(this); 
       option.setImageResource(questionImages[0][j]); 
       option.setId(j + (10 * (i + 1))); 
       RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
         RelativeLayout.LayoutParams.WRAP_CONTENT, 
         RelativeLayout.LayoutParams.WRAP_CONTENT); 
       params.addRule(RelativeLayout.RIGHT_OF, j - 1 
         + (10 * (i + 1))); 
       option.setLayoutParams(params); 
       r1.addView(option, params); 
      } 
      break; 
     } 
    } 
    s1.addView(r1, new LayoutParams(LayoutParams.FILL_PARENT, 
      LayoutParams.FILL_PARENT)); 
    this.setContentView(s1); 
} 
} 

图2:https://www.dropbox.com/s/itazcpshjzbza4t/pic1.png

当环路在开关壳体与变量'我'只运行一次,秒ond输出。如果我运行两次,第一个输出会出现。但那不是我想要的。我想要第一个输出行显示在第二个输出行的下面。我知道代码中有些问题,但我不知道如何纠正它。请帮帮我!另外,我想知道如何将这些元素放置在屏幕上的任何位置。

P.S.我没有被允许上传图片,因为我的声望低于10(我在这里是新手)。所以,我把它们放在Dropbox上,并把链接放在这里。抱歉给你带来不便。

+0

为什么不使用带适配器的列表? –

+0

对不起,但请您详细说明一下吗? – pratnala

+0

对不起,我没有正确理解你的问题。不要介意我说的话。 –

回答

1

我已经解决了这个问题:我之前只指定了RIGHT_OF属性,因此它不知道垂直放置的位置,因此放在顶部。指定BELOW属性也解决了问题。

+0

我之前只指定了RIGHT_OF属性,因此它不知道垂直放置的位置,因此将它放在顶部。通过指定BELOW属性,我解决了它。我可以收到upvote吗?需要一些代表在未来的问题中包含图像。 – pratnala