PercentRelativeLayout inside ScrollView

后端 未结 4 2088
忘掉有多难
忘掉有多难 2021-01-07 22:58

I have created a layout using a ScrollView which has a PercentRelativeLayout as its child. It doesn\'t work on Lollipop and older devices but works

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-07 23:24

    I encountered the same problem with android K and android L.

    You could have used constraint layout instead of percent relative layout but there seems scrolling problem with that too. Even custom Scroll View as suggested in other answers wasn't helpful for me.

    Though there's a way.

    Convert your percent relative layout to relative layout and set the height of the views programmatically according to your device height.

    There seems no problem in scrolling with relative layout. And you can use 'Space' view for margin between TextViews. This is not a very elegant solution but for me it worked.

    Java Code:

    int height = getWindowManager().getDefaultDisplay().getHeight();
    int width = getWindowManager().getDefaultDisplay().getWidth();
    
    // to set height to each textview to 10% of screen
    textView1.getLayoutParams().height = (int) (height * 0.10);
    textView2.getLayoutParams().height = (int) (height * 0.10);
    textView3.getLayoutParams().height = (int) (height * 0.10);
    textView4.getLayoutParams().height = (int) (height * 0.10);
    textView5.getLayoutParams().height = (int) (height * 0.10);
    textView6.getLayoutParams().height = (int) (height * 0.10);
    
    // to set width to each textview to 50% of screen
    textView1.getLayoutParams().width = (int) (width * 0.50);
    textView2.getLayoutParams().width = (int) (width * 0.50);
    textView3.getLayoutParams().width = (int) (width * 0.50);
    textView4.getLayoutParams().width = (int) (width * 0.50);
    textView5.getLayoutParams().width = (int) (width * 0.50);
    textView6.getLayoutParams().width = (int) (width * 0.50);
    
    // for margin between textviews
    space1.getLayoutParams().height = (int) (height * 0.10);
    space2.getLayoutParams().height = (int) (height * 0.10);
    space3.getLayoutParams().height = (int) (height * 0.10);
    space4.getLayoutParams().height = (int) (height * 0.10);
    space5.getLayoutParams().height = (int) (height * 0.10);
    

    XML Code:

    
    
    
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
    
    

提交回复
热议问题