简体   繁体   English

相对布局android loopable

[英]Relative layout android loopable

I am writing an app that loads up a row of buttons (2 buttons then a black line underneath) for each file that is found (so loop count will not be static). 我正在编写一个应用程序,为找到的每个文件加载一行按钮(2个按钮,然后在其下是黑线)(因此循环计数将不是静态的)。 Currently while building I have a static loop count to 15. But when running the code it creates the bigger button on the left and the black line underneath fine... But... The smaller button on the right only appears once. 目前,在构建时,我的静态循环数为15。但是在运行代码时,它将在左侧创建一个较大的按钮,并且在其下方创建一个黑线。但是...在右侧,较小的按钮仅显示一次。 Any idea why? 知道为什么吗?

    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);

}

Honestly, this seems like overkill to do all this in code. 老实说,在代码中完成所有这些工作似乎有些过头了。 I'd recommend creating a layout file, and inflating that multiple times. 我建议创建一个布局文件,并对其进行多次充气。 Make appLayout a LinearLayout with vertical orientation, it would be as simple as this: 将appLayout设为具有垂直方向的LinearLayout,就可以像这样简单:

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); 
}

and your layout file would be relatively simple too: 并且您的布局文件也将相对简单:

<LinearLayout android:orientation="vertical" ...>
  <LinearLayout android:orientation="horizontal" ...>
    <Button android:id="@+id/button1" ... />
    <Button android:id="@+id/button2" ... />
  </LinearLayout>
  <View android:background="@android:color/black"
        android:layout_height="3dp"
        android:layout_width="match_parent" />
</LinearLayout>

While laying out elements in code is definitely do-able, it can be a headache to get right. 尽管在代码中布置元素绝对是可行的,但正确做起来可能很头疼。 I find it easier to use XML files, and inflate them as needed like this. 我发现使用XML文件更容易,并按需要将它们膨胀。 My guess is the way you're using your ids is confusing the layout. 我的猜测是您使用ID的方式会混淆布局。 It's probably best to let buttons get their own id (don't use setId), and use the "getId" method when setting your relative layout params. 最好让按钮获取自己的ID(不要使用setId),并在设置相对布局参数时使用“ getId”方法。

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

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