Pergunta

Could some help me with Layouts? I'm having a problem getting it to display the way I'd like.

I have two relativelayouts in a linearlayout. RelativeLayout 1 is used to accomodate a fragment, RelativeLayout 2 contains the 'main' layout that should fill the screen when there is no fragment, but resize when the fragment is added.

I create the layouts dynamically like so:

    LinearLayout mainLayout = new LinearLayout(this);
    mainLayout.setLayoutDirection(LinearLayout.VERTICAL);

    unityPlayerLayout = new RelativeLayout(this);
    youtubeLayout = new RelativeLayout(this);


    LinearLayout.LayoutParams mainParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);

    mainLayout.setLayoutParams(mainParams);


    RelativeLayout.LayoutParams youtubeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,600);

    RelativeLayout.LayoutParams unityPlayerLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.MATCH_PARENT);
    unityPlayerLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);


    mainLayout.addView(youtubeLayout,0,youtubeLayoutParams);
    mainLayout.addView(unityPlayerLayout);
    unityPlayerLayout.addView(playerView,0,unityPlayerLayoutParams);

Upon adding the fragment, unityPlayerLayout does not resize and aligned to the bottom though. It get's pushed to the right, I can see a sliver of a couple of pixels, which is weird, since youtubeLayout and mainLayout should match the screen.

So, to summarize: Upon adding a fragment to youtubeLayout, I need unityPlayerLayout to resize it's height and drop to the bottom, but in practice unityPlayerLayout gets pushed to the right, and does not resize it's height.

Anyone any idea? Much appreciated!

Foi útil?

Solução

You nee to set Layout orientation for the main LinearLayout, not direction

Change

mainLayout.setLayoutDirection(LinearLayout.VERTICAL);

to

mainLayout.setOrientation(LinearLayout.VERTICAL);

Outras dicas

// try this way,hope this will help you...
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout mainLayout = new LinearLayout(this);
        RelativeLayout unityPlayerLayout = new RelativeLayout(this);
        unityPlayerLayout.setBackgroundColor(getResources().getColor(android.R.color.holo_red_dark));
        RelativeLayout youtubeLayout = new RelativeLayout(this);
        youtubeLayout.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));

        mainLayout.setOrientation(LinearLayout.VERTICAL);
        mainLayout.addView(youtubeLayout,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,600));
        mainLayout.addView(unityPlayerLayout,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1f));
        setContentView(mainLayout,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top