简体   繁体   中英

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). 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. 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:

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. My guess is the way you're using your ids is confusing the layout. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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