简体   繁体   中英

Dynamically-generated LinearLayouts overlapping

I'm a newbie to Android development, and I'm very much still learning Java too so be gentle!

I am creating an app that can take information about a task (I'm basing it around a sort of homework planner), store that info and then display it in a list. The program must be able to dynamically generate the list from the background files. I have managed all of this so far, but when I create a basic output for each task, containing the "subject" and "details" variables using a LinearLayout they appear on the screen overlapping. They all seem to be creating correctly, but they are all being put in the same place. Are there attributes I can set to make them display in a vertical list???

Here is the piece of code where I generate the viewgroups and display them. This is called from a loop in another part of the program which finds the number of files in internal storage.

    TextView subjView;
    TextView detailView;
    RelativeLayout displayLayout;

    LinearLayout taskDisplay = new LinearLayout(this);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);


    subjView = new TextView(this);      
    detailView = new TextView(this);
    displayLayout = (RelativeLayout) findViewById(R.id.relative_display_layout);

    subjView.setText(subject);
    detailView.setText(details);

    layoutParams.setMargins(0, 0, 0, 0);
    taskDisplay.addView(subjView, layoutParams);

    layoutParams.setMargins(10, 0, 0, 0);
    taskDisplay.addView(detailView, layoutParams);

    displayLayout.addView(taskDisplay);

If I understand correctly, I think your issue is only that you are declaring and then changing the layoutParams margins which sets them both to the same, which is overlapping your TextViews .


Edit

Okay, I am still not 100% sure how you are doing all of this so my example may need to be tweaked. I tried to throw this together quickly so forgive me for any minor mistakes.

New mock up for dynamic layouts:

TextView subjView, detailView;
RelativeLayout displayLayout, rl;

// I am assuming this is your main layout
displayLayout = (RelativeLayout) findViewById(R.id.relative_display_layout);

// Just using a for loop as an example of a loop event, not sure how you are accomplishing this
for(int i = 0; i < data.length(); i++) {

    RelativeLayout.LayoutParams rllp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 100);

    if (i > 0) {
        int rePositionRule = i;
        rllp.addRule(RelativeLayout.BELOW, rePositionRule);
    }

    RelativeLayout taskDisplay = new RelativeLayout(this);

    taskDisplay.setLayoutParams(rllp);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(0, 0, 0, 0);

    LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParams2.setMargins(10, 0, 0, 0);

    subjView = new TextView(this);      
    detailView = new TextView(this);


    subjView.setText(subject);
    subjView.setLayoutParams(layoutParams);
    detailView.setText(details);
    detailView.setLayoutParams(layoutParams2);


    taskDisplay.addView(subjView);
    taskDisplay.addView(detailView);

    displayLayout.addView(taskDisplay);

}

Your displayLayout is a relativeLayout.. A relative layout, as the name implies, places element relative to each other. Normally you'd say "element A should go below element B" etc. Since you aren't providing any of these rules for the items you are creating they are just going to all go to the default position in a relative layout (which is the top of the screen.. hence the overlap)

If you don't want to deal with the hassle of changing your code to place things relatively simply switch your displayLayout to a LinearLayout in your xml and code and set its orientation to vertical. You'll probably want to wrap that in a scroll view if it runs off the screen

However, it sounds like what you really want is a ListView...

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