简体   繁体   English

缩放孩子后如何修复滚动视图大小?

[英]How to fix scrollview size after scaling child?

I am trying to zoom out a GridLayout that is inside a HorizontalScrollView.我正在尝试缩小 Horizo​​ntalScrollView 内的 GridLayout。 When the GridLayout is scaled (50%), the HorizontalScrollView keep the original size, which leads to a lot of blank space. GridLayout 缩放(50%)时,Horizo​​ntalScrollView 保持原来的大小,导致大量空白。 How can I adjust the HorizontalScrollView size so that it fits the scaled GridLayout?如何调整 Horizo​​ntalScrollView 大小以使其适合缩放的 GridLayout?

Here is a simplified version of what I am trying to do so that it is easier to understand:这是我正在尝试做的事情的简化版本,以便更容易理解:

Java code:爪哇代码:

public class MainActivity extends AppCompatActivity {

    GridLayout gridLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gridLayout = (GridLayout) findViewById(R.id.gridlayout);

    }

    public void zoomButton(View v){
        gridLayout.setScaleX(0.5f);
        gridLayout.setScaleY(0.5f);
    }

}

XML: XML:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relative_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <HorizontalScrollView
            android:id="@+id/horizontalscrollview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <GridLayout
                android:id="@+id/gridlayout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:columnCount="5"
                android:transformPivotX="0dp"
                android:transformPivotY="0dp"
                tools:context=".MainActivity">

                <TextView
                    android:id="@+id/txt"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#335BF1"
                    android:padding="48dp"
                    android:text="NUMBER 01" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#34C40C"
                    android:padding="48dp"
                    android:text="NUMBER 02" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#335BF1"
                    android:padding="48dp"
                    android:text="NUMBER 03" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#34C40C"
                    android:padding="48dp"
                    android:text="NUMBER 04" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#335BF1"
                    android:padding="48dp"
                    android:text="NUMBER 05" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#34C40C"
                    android:padding="48dp"
                    android:text="NUMBER 06" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#335BF1"
                    android:padding="48dp"
                    android:text="NUMBER 07" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#34C40C"
                    android:padding="48dp"
                    android:text="NUMBER 08" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#335BF1"
                    android:padding="48dp"
                    android:text="NUMBER 09" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#34C40C"
                    android:padding="48dp"
                    android:text="NUMBER 10" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#335BF1"
                    android:padding="48dp"
                    android:text="NUMBER 11" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#34C40C"
                    android:padding="48dp"
                    android:text="NUMBER 12" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#335BF1"
                    android:padding="48dp"
                    android:text="NUMBER 13" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#34C40C"
                    android:padding="48dp"
                    android:text="NUMBER 14" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#335BF1"
                    android:padding="48dp"
                    android:text="NUMBER 15" />

            </GridLayout>

        </HorizontalScrollView>

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_margin="16dp"
        android:onClick="zoomButton"
        android:text="ZoomOut" />

</RelativeLayout>

Before Zooming Out:缩小前:

缩小前:

After Zooming Out (manually scrolled to show more of the blank space):缩小后(手动滚动以显示更多空白区域):

缩小后:

The size of the view does not change when the scale is changed.更改比例时,视图的大小不会更改。

When you zoom in your grid layout, the reason why your horizontalscroll view does not change its size is because for HorizontalScrollView, the size of gridlayout has not changed at all.当你放大你的网格布局时,你的水平滚动视图没有改变其大小的原因是因为对于 Horizo​​ntalScrollView,gridlayout 的大小根本没有改变。

How can I adjust the HorizontalScrollView size so that it fits the scaled GridLayout?如何调整 Horizo​​ntalScrollView 大小以使其适合缩放的 GridLayout?

you will have to change the size of your grid layout at the same time when you focus.您必须在聚焦的同时更改网格布局的大小。 This will not be easy as i can see that you are using the padding in your tables.这并不容易,因为我可以看到您正在使用表格中的填充。 So, when you decrease the size of your tables in gridlayout, your padding will misbehave and you will have incorrect view.因此,当您在 gridlayout 中减小表格的大小时,您的填充会出现异常并且您将获得不正确的视图。

The minimum that can be done in your case is that you can change the height of HorizontalScrollView, for width you will have to deal with padding issues.在您的情况下可以完成的最低限度是您可以更改 Horizo​​ntalScrollView 的高度,对于宽度,您将不得不处理填充问题。

Code for Height Change高度变化代码

MainActivity主要活动

public class MainActivity extends AppCompatActivity {

    GridLayout gridLayout;
    HorizontalScrollView horizontalScrollView;
    RelativeLayout relative_layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gridLayout = (GridLayout) findViewById(R.id.gridlayout);
        horizontalScrollView = (HorizontalScrollView) findViewById(R.id.horizontalscrollview);
        relative_layout = (RelativeLayout) findViewById(R.id.relative_layout);
    }

    public void zoomButton(View v){
        gridLayout.setScaleY(0.5f);
        gridLayout.setScaleX(0.5f);

        FrameLayout.LayoutParams layout1 = new FrameLayout.LayoutParams(gridLayout.getWidth(), gridLayout.getHeight());
        gridLayout.setLayoutParams(layout1);

        RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(horizontalScrollView.getWidth(), horizontalScrollView.getHeight()/2);
        horizontalScrollView.setLayoutParams(layout);
    }
}

I found a better way of dealing with the blank space other then trying to change the view size dinamically.我找到了一种更好的处理空白的方法,然后尝试动态地更改视图大小。

I added listeners for scroll changes and limited the scrolling according to the math below (considering that I am scaling by 0.5f):我添加了滚动更改的侦听器,并根据下面的数学公式限制了滚动(考虑到我的缩放比例为 0.5f):

sView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
    @Override
    public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

        if (scrollY >= gridLayout.getHeight/2-sView.getHeight){
            sView.setScrollY(gridLayout.getHeight/2-sView.getHeight);
        }

    }
});

hView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
    @Override
    public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

        if (scrollX >= gridLayout.getWidth/2-hView.getWidth){
            hView.setScrollX(gridLayout.getWidth/2-hView.getWidth);
        }

    }
});

It solved my issue, I hope it's useful for someone else.它解决了我的问题,我希望它对其他人有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM