Java 相对布局可循环

Java 相对布局可循环,java,android,layout,screen,Java,Android,Layout,Screen,我正在编写一个应用程序,为找到的每个文件加载一行按钮(2个按钮,然后在下面画一条黑线)(这样循环计数就不会是静态的)。目前,在构建时,我的静态循环计数为15。但是当运行代码时,它会在左边创建一个更大的按钮,在下面创建一条黑线。。。但是右侧较小的按钮只显示一次。知道为什么吗 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ScrollView s

我正在编写一个应用程序,为找到的每个文件加载一行按钮(2个按钮,然后在下面画一条黑线)(这样循环计数就不会是静态的)。目前,在构建时,我的静态循环计数为15。但是当运行代码时,它会在左边创建一个更大的按钮,在下面创建一条黑线。。。但是右侧较小的按钮只显示一次。知道为什么吗

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ScrollView scrollPictures = new ScrollView(this);
    RelativeLayout appLayout = new RelativeLayout(this);
   // appLayout.setClipBounds(null);
    Resources r = getResources();
    ImageView blackLine;

    RelativeLayout.LayoutParams p;
    int id = 1;
    for(int x = 1; x <= 15; x++){
        p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        Button studentsButton = new Button(this);
        studentsButton.setClipBounds(null);
        studentsButton.setId(id);
        studentsButton.setHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, r.getDisplayMetrics()));
        studentsButton.setWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 700, r.getDisplayMetrics()));
        //studentsButton.setBackgroundColor(Color.LTGRAY);
        if (x > 1 ){
            p.addRule(RelativeLayout.BELOW, id - 1);
            studentsButton.setLayoutParams(p);
        }

        appLayout.addView(studentsButton);
        id ++;

        p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        Button soundButton = new Button(this);
        soundButton.setClipBounds(null);
        soundButton.setHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, r.getDisplayMetrics()));
        soundButton.setWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, r.getDisplayMetrics()));
        //soundButton.setBackgroundColor(Color.LTGRAY);
        p.addRule(RelativeLayout.RIGHT_OF, id - 1);
        soundButton.setLayoutParams(p);
        appLayout.addView(soundButton);

        p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        blackLine = new ImageView(this);
        blackLine.setId(id);
        blackLine.setBackgroundColor(Color.BLACK);
        blackLine.setMinimumWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 700, r.getDisplayMetrics()));
        blackLine.setMinimumHeight(3);
        blackLine.setMinimumWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 700, r.getDisplayMetrics()));
        blackLine.setClipBounds(null);
        p.addRule(RelativeLayout.BELOW, id - 1);
        blackLine.setLayoutParams(p);
        appLayout.addView(blackLine);
        id++;


    }

    scrollPictures.addView(appLayout);
    setContentView(scrollPictures);

}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
ScrollView scrollPictures=新的ScrollView(此);
RelativeLayout appLayout=新的RelativeLayout(此);
//appLayout.setClipBounds(null);
Resources r=getResources();
图像视图黑线;
RelativeLayout.LayoutParams p;
int-id=1;
对于(int x=1;x 1){
p、 addRule(RelativeLayout.down,id-1);
学生按钮。设置布局参数(p);
}
addView(学生按钮);
id++;
p=新的RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_内容,
ViewGroup.LayoutParams.WRAP_内容);
按钮声音按钮=新按钮(此按钮);
soundButton.setClipBounds(空);
soundButton.setHeight((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,100,r.getDisplayMetrics());
soundButton.setWidth((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,100,r.getDisplayMetrics());
//soundButton.setBackgroundColor(Color.LTGRAY);
p、 addRule(RelativeLayout.RIGHT_OF,id-1);
soundButton.setLayoutParams(p);
appLayout.addView(声音按钮);
p=新的RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_内容,
ViewGroup.LayoutParams.WRAP_内容);
黑线=新图像视图(此);
blackLine.setId(id);
黑线.背景色(颜色.黑色);
blackLine.setMinimumWidth((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,700,r.getDisplayMetrics());
黑线。设置最小高度(3);
blackLine.setMinimumWidth((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,700,r.getDisplayMetrics());
blackLine.setClipBounds(null);
p、 addRule(RelativeLayout.down,id-1);
blackLine.setLayoutParams(p);
appLayout.addView(黑线);
id++;
}
滚动图片。添加视图(appLayout);
设置内容视图(滚动图片);
}

老实说,在代码中完成这一切似乎有些过分。我建议创建一个布局文件,并将其膨胀多次。将appLayout设置为具有垂直方向的线性布局,简单如下:

for(int x = 1; x <= 15; x++){
  View view = LayoutInflater.from(this).inflate(R.layout.some_layout, appLayout, false);

  // Configure the items inside the view

  Button button1 = (Button)view.findViewById(R.id.button1);
  button1.setText("Button 1");
  button1.setOnClickListener(new View.OnClickListener() { ... });

  ...

  appLayout.addView(view); 
}

for(int x=1;x老实说,在代码中完成这一切似乎有些过分。我建议创建一个布局文件,并将其膨胀多次。将appLayout设置为具有垂直方向的线性布局,就这么简单:

for(int x = 1; x <= 15; x++){
  View view = LayoutInflater.from(this).inflate(R.layout.some_layout, appLayout, false);

  // Configure the items inside the view

  Button button1 = (Button)view.findViewById(R.id.button1);
  button1.setText("Button 1");
  button1.setOnClickListener(new View.OnClickListener() { ... });

  ...

  appLayout.addView(view); 
}

for(int x=1;x我尝试按您的方式执行,但“视图”的定义会导致错误。在开始时,“getContext()”不是可以解析的方法。然后,如果我使用“getApplicationContext()”,则会导致错误;from()在LayoutInflater中无法应用于……有什么想法吗?这取决于调用它的来源。如果您在某个活动中,只需使用“this”即可,我得到的错误与LayoutInflater中的“from()无法应用于”……必需:上下文,已找到:MainActivity,LinearLayout,boolean,原因:实际参数列表和形式参数列表长度不同抱歉,缺少参数。正在修复。要执行“充气(R.id.some_布局…”,您需要为布局提供id…我添加了-android:id=“@+id/some_布局”-作为-下的测试,我尝试按您的方式执行,但“视图”的定义会导致错误。在开始时,“getContext()”不是可以解决的方法。然后,如果我使用“getApplicationContext()”,则会导致错误;from()在LayoutInflater中无法应用于……有什么想法吗?这取决于调用它的来源。如果您在某个活动中,只需使用“this”即可,我得到的错误与LayoutInflater中的“from()无法应用于”…必需:上下文,找到:MainActivity,LinearLayout,boolean,原因:实际参数列表和正式参数列表长度不同抱歉,缺少参数。修复它。要执行“充气(R.id.some_布局…”操作,您需要为布局提供id…我添加了-android:id=“@+id/some_布局”-作为下一个测试-