How does Android scroll only when the content is at full screen height and center when it is not?

  android, question

As shown in the following figure:

图1图2

The quantity of content in Card content is not fixed. Card header and Card footer are fixed in content.

When the content is small and the screen is high (fig. 1), you want to display all the content with the entire Card vertically centered on the screen.

While the content is more and the screen is smaller (fig. 2), when the content cannot be displayed completely on one screen, let the Card fill up the screen, while the Card content part uses ScrollView to display the content.

Is there any elegant and concise way to do this? If you want to implement a View by yourself, is there any general idea for reference?

Thank you

The great god Mariotaku has helped me solve this problem. According toThe train of thought he wroteI sorted it out:

First of all, I set the gravity of a RelativeLayout full of the whole screen as the center, and then the whole card will naturally be centered inside. Card contains three View, namely header, ScrollView and footer in the middle.

The Card part is implemented by itself and is called CardLayout:

class CardLayout extends ViewGroup {
 
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 
 int w = MeasureSpec.getSize(widthMeasureSpec);
 int h = MeasureSpec.getSize(heightMeasureSpec);
 
 // measure card header
 measureChild(getChildAt(0), widthMeasureSpec, heightMeasureSpec);
 // measure the card footer.
 measureChild(getChildAt(2), widthMeasureSpec, heightMeasureSpec);
 
 //Calculate the maximum height that the card content part can occupy.
 int cardMaxHeight = h - getChildAt(0).getMeasuredHeight() - getChildAt(2).getMeasuredHeight();
 
 // measure the card content.
 measureChild(getChildAt(1), getChildMeasureSpec(widthMeasureSpec, 0, w), getChildMeasureSpec(MeasureSpec.makeMeasureSpec(cardMaxHeight, MeasureSpec.AT_MOST), 0, cardMaxHeight));
 
 //Height of entire card
 Setmeasureddimension (w, getchildat (0). getmeasuredheit () plus getchildat (1). getmeasuredheit () plus getchildat (2). getmeasuredheit ());
 
 bracket
 
 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
 //layout the three child separately
 getChildAt(0).layout(0, 0, getChildAt(0).getMeasuredWidth(), getChildAt(0).getMeasuredHeight());
 Getchildat (1). layout (0, getchildat (0). getbottom (), getchildat (1). getmeasurewidth (), getchildat (0). getbottom () plus getchildat (1). getmeasuredheit ());
 Getchildat (2). layout (0, getchildat (1). getbottom (), getchildat (2). getmeasurewidth (), getchildat (1). getbottom () plus getchildat (2). getmeasuredheit ());
 bracket
 
 bracket