Android– 如何缩放视图以完全适合全屏应用程序的高度和宽度?

Android– 如何缩放视图以完全适合全屏应用程序的高度和宽度?,第1张

概述如果您有一个计算器应用程序,并且您想要编写一个看起来像this的布局,那么如何缩放按钮和显示以适合所有屏幕尺寸?我研究过的想法:>以编程方式计算每个组件的高度和宽度.以编程方式创建视图可以为您提供最大的功能,但并不完全理想.我更喜欢用XML编写我的UI.>使用布局权重嵌套Linear

如果您有一个计算器应用程序,并且您想要编写一个看起来像this的布局,那么如何缩放按钮和显示以适合所有屏幕尺寸?

我研究过的想法:

>以编程方式计算每个组件的高度和宽度.以编程方式创建视图可以为您提供最大的功能,但并不完全理想.我更喜欢用XML编写我的UI.
>使用布局权重嵌套linearLayouts.这是有效的,但lint给出了性能警告,因为我正在嵌套权重.最重要的是,它没有考虑文本大小.所以在小屏幕上,文字被砍掉了.相反,在大屏幕上,文字太小.

编辑:
3.使用具有嵌套权重的tableLayout.考虑到这些从linearLayout扩展,我认为缺乏lint警告是无关紧要的,这仍然会导致性能损失吗?

有没有更好的办法?我觉得我错过了一些明显的东西

编辑2:
如果有人对此解决方案感兴趣,我已经创建了一个自定义布局(如raphw建议)并将在此处发布源代码:

EvenSpaceGrIDLayout.java:

package com.example.evenspacegrIDlayout;import androID.content.Context;import androID.content.res.TypedArray;import androID.util.AttributeSet;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;public class EvenSpaceGrIDLayout extends VIEwGroup {    private int mNumColumns;    public EvenSpaceGrIDLayout(Context context) {        super(context);    }    public EvenSpaceGrIDLayout(Context context, AttributeSet attrs) {        super(context, attrs);        TypedArray a = context.obtainStyledAttributes(attrs,                R.styleable.EvenSpaceGrIDLayout);        try {            mNumColumns = a.getInteger(                    R.styleable.EvenSpaceGrIDLayout_num_columns, 1);        } finally {            a.recycle();        }    }    @OverrIDe    protected voID onMeasure(int wIDthMeasureSpec, int heightmeasureSpec) {        int wIDthSize = MeasureSpec.getSize(wIDthMeasureSpec);        int heightSize = MeasureSpec.getSize(heightmeasureSpec);        // Calculate how many cells we need        int cellCount = countCellsNeeded();        // Calculate number of rows needed given the number of cells        int numRows = cellCount / mNumColumns;        // Calculate wIDth/height of each indivIDual cell        int cellWIDth = wIDthSize / mNumColumns;        int cellHeight = heightSize / numRows;        // Measure children        measureChildrenVIEws(cellWIDth, cellHeight);        setMeasuredDimension(wIDthMeasureSpec, heightmeasureSpec);    }    @OverrIDe    protected voID onLayout(boolean changed, int l, int t, int r, int b) {        final int count = getChildCount();        for (int i = 0; i < count; i++) {            VIEw child = getChildAt(i);            LayoutParams lp = (LayoutParams) child.getLayoutParams();            child.layout(lp.x, lp.y, lp.x + child.getMeasureDWIDth(), lp.y + child.getMeasuredHeight());        }    }    private int countCellsNeeded() {        int cellCount = 0;        final int childCount = getChildCount();        for (int i = 0; i < childCount; i++) {            VIEw child = getChildAt(i);            LayoutParams lp = (LayoutParams) child.getLayoutParams();            int spanColumns = lp.spanColumns;            // If it's trying to span too far, make it span the maximum possible            if (spanColumns > mNumColumns) {                spanColumns = mNumColumns;            }            int remainingCellsInRow = mNumColumns - (cellCount % mNumColumns);            if (remainingCellsInRow - spanColumns < 0) {                cellCount += remainingCellsInRow + spanColumns;            } else {                cellCount += spanColumns;            }        }        // Round off the last row        if ((cellCount % mNumColumns) != 0) {            cellCount += mNumColumns - (cellCount % mNumColumns);        }        return cellCount;    }    private voID measureChildrenVIEws(int cellWIDth, int cellHeight) {        int cellCount = 0;        final int childCount = getChildCount();        for (int i = 0; i < childCount; i++) {            VIEw child = getChildAt(i);            LayoutParams lp = (LayoutParams) child.getLayoutParams();            int spanColumns = lp.spanColumns;            // If it's trying to span too far, make it span the maximum possible            if (spanColumns > mNumColumns) {                spanColumns = mNumColumns;            }            // If it can't fit on the current row, skip those cells            int remainingCellsInRow = mNumColumns - (cellCount % mNumColumns);            if (remainingCellsInRow - spanColumns < 0) {                cellCount += remainingCellsInRow;            }            // Calculate x and y coordinates of the vIEw            int x = (cellCount % mNumColumns) * cellWIDth;            int y = (cellCount / mNumColumns) * cellHeight;            lp.x = x;            lp.y = y;            child.measure(MeasureSpec.makeMeasureSpec(cellWIDth * spanColumns, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(cellHeight, MeasureSpec.EXACTLY));            cellCount += spanColumns;        }    }    @OverrIDe    protected boolean checkLayoutParams(VIEwGroup.LayoutParams p) {        return p instanceof LayoutParams;    }    @OverrIDe    protected LayoutParams generateDefaultLayoutParams() {        return new LayoutParams(LayoutParams.WRAP_CONTENT,                LayoutParams.WRAP_CONTENT);    }    @OverrIDe    public LayoutParams generateLayoutParams(AttributeSet attrs) {        return new LayoutParams(getContext(), attrs);    }    @OverrIDe    protected LayoutParams generateLayoutParams(VIEwGroup.LayoutParams p) {        return new LayoutParams(p.wIDth, p.height);    }    public static class LayoutParams extends VIEwGroup.LayoutParams {        int x, y;        public int spanColumns;        public LayoutParams(Context context, AttributeSet attrs) {            super(context, attrs);            TypedArray a = context.obtainStyledAttributes(attrs,                    R.styleable.EvenSpaceGrIDLayout_LayoutParams);            try {                spanColumns = a                        .getInteger(                                R.styleable.EvenSpaceGrIDLayout_LayoutParams_span_columns,                                1);                // Can't span less than one column                if (spanColumns < 1) {                    spanColumns = 1;                }            } finally {                a.recycle();            }        }        public LayoutParams(int w, int h) {            super(w, h);        }    }}

attrs.xml:

<?xml version="1.0" enCoding="utf-8"?><resources>    <declare-styleable name="EvenSpaceGrIDLayout">        <attr name="num_columns" format="integer" />    </declare-styleable>    <declare-styleable name="EvenSpaceGrIDLayout_LayoutParams">        <attr name="span_columns" format="integer" />    </declare-styleable></resources>

用法如下:

<com.example.evenspacegrIDlayout.EvenSpaceGrIDLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:grID="http://schemas.androID.com/apk/res/com.example.evenspacegrIDlayout"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    grID:num_columns="4" >    <button        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:gravity="center"        androID:text="CL" />    <button        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:gravity="center"        androID:text="Del" />    <!-- empty cell -->    <VIEw         androID:layout_wIDth="0dp"        androID:layout_height="0dp" />    <button        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:gravity="center"        androID:text="/" />    <button        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:gravity="center"        androID:text="7" />    <button        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:gravity="center"        androID:text="8" />    <button        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:gravity="center"        androID:text="9" />    <button        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:gravity="center"        androID:text="*" />    <button        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:gravity="center"        androID:text="4" />    <button        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:gravity="center"        androID:text="5" />    <button        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:gravity="center"        androID:text="6" />    <button        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:gravity="center"        androID:text="-" />    <button        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:gravity="center"        androID:text="1" />    <button        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:gravity="center"        androID:text="2" />    <button        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:gravity="center"        androID:text="3" />    <button        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:gravity="center"        androID:text="+" />    <button        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:gravity="center"        androID:text="." />    <button        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:gravity="center"        androID:text="0" />    <button        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:gravity="center"        androID:text="="        grID:span_columns="2" /></com.example.evenspacegrIDlayout.EvenSpaceGrIDLayout>

最终结果是:

解决方法:

您可以自己编写一个VIEwGroup子类,它可以执行您想要的 *** 作,并且仍然可以在XML布局定义中使用此布局,就像使用任何预定义布局一样.或者,查看GrIDLayout类.也许这个VIEwGroup实现已经完成了你想要的. (http://developer.android.com/reference/android/widget/GridLayout.html)最后,这些VIEwGroup布局以编程方式计算其包含的VIEw组件的大小,如果没有预定义的布局提供您需要的功能,除了实现您的个性化要求之外别无他法.

但是,按钮VIEw实例仍应负责将其内容保持在最近调用onMeasure期间收到的大小范围内.

将布局嵌套到扩展中,确实应该避免使用示例图片和linearLayout类.应该注意的是,这仍然是一种常见的做法,因为这在使用tableLayout时是隐式完成的.

总结

以上是内存溢出为你收集整理的Android – 如何缩放视图以完全适合全屏应用程序的高度和宽度?全部内容,希望文章能够帮你解决Android – 如何缩放视图以完全适合全屏应用程序的高度和宽度?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1106606.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存