You do not want your view to be fifth in height of the screen; you want it to be one fifth of the height of the parent view. Or, to be precise, you want your width to be one fifth of the height of your views.
The difference is that using the width / height of the screen may work on your device, but break on others. What would you do if someone opened your split-screen application?
However, determining the size of your presentation, the approach is the same: if you want a presentation that follows certain rules, you will need to create your own and evaluate your presentation accordingly.
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int availableHeight = MeasureSpec.getSize(heightMeasureSpec); int wantedWidth = availableHeight / 5; setMeasuredDimension(wantedWidth, availableHeight); }
And thatβs basically it. You might want to read the correct documentation about MeasureSpec and measurements in general. I also wrote a blog post about custom views covering some of the basics.
Then you can simply add your custom views to LinearLayout by supporting ScrollView and you will install.
Here, a complete sample that will work mainly, you may need to use something other than a frame layout.
public class FifthWidthView extends FrameLayout { public FifthWidthView(Context context) { super(context); } public FifthWidthView(Context context, AttributeSet attrs) { super(context, attrs); } public FifthWidthView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int availableHeight = MeasureSpec.getSize(heightMeasureSpec); int wantedWidth = availableHeight / 5; setMeasuredDimension(wantedWidth, availableHeight); } }
David Medenjak
source share