以编程方式更改视图边距

时间:2020-07-21 10:15:06

标签: java android android-layout

所以我有一个RecyclerView,底部边距为250dp 而且我需要以编程方式将底部页边距设置为0dp,然后将其设置回250dp,我已经尝试过了,但是它只是将其设置为0dp,而将其设置为250dp却不起作用:

    ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) linearLayoutRv.getLayoutParams();
                params.setMargins(0,0,0,250); // params.setMargins(0,0,0,0); at first
                linearLayoutRv.setLayoutParams(params);

有些不必要的事情,但这是xml文件:

    <androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/linearLayoutRv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="250dp"
        android:gravity="center"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="51dp"
            android:gravity="center">
            //textview
          
        </androidx.constraintlayout.widget.ConstraintLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="51dp"
            android:orientation="horizontal">
            //some buttons
        </LinearLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginHorizontal="5dp"
            android:padding="4dp"
            android:scrollbars="vertical" />
    </LinearLayout>

4 个答案:

答案 0 :(得分:0)

问题是setMargins接受的值不是以dp(密度无关的像素)为单位,而是以px(像素)为单位。

您必须先将250的值转换为dp,然后再将其传递给params对象。

Context context = ...; // place 'this' if you are in an Activity or 'requireContext()' if you are in a Fragment
Resources res = context.getResources();
float dp250 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 250, res.getDisplayMetrics());

ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) linearLayoutRv.getLayoutParams();
params.setMargins(0, 0, 0, dp250);
linearLayoutRv.setLayoutParams(params);

dp250将在不同设备上产生不同数量的像素。这取决于分辨率和物理屏幕尺寸。

答案 1 :(得分:0)

以编程方式设置边距时,以像素为单位而不是dp来设置值。 LayoutParams的来源:

         /**
         * Sets the margins, in pixels. A call to {@link android.view.View#requestLayout()} needs
         * to be done so that the new margins are taken into account. Left and right margins may be
         * overridden by {@link android.view.View#requestLayout()} depending on layout direction.
         * Margin values should be positive.
         *
         * @param left the left margin size
         * @param top the top margin size
         * @param right the right margin size
         * @param bottom the bottom margin size
         *
         * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginLeft
         * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop
         * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginRight
         * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom
         */
        public void setMargins(int left, int top, int right, int bottom) {
            leftMargin = left;
            topMargin = top;
            rightMargin = right;
            bottomMargin = bottom;
            mMarginFlags &= ~LEFT_MARGIN_UNDEFINED_MASK;
            mMarginFlags &= ~RIGHT_MARGIN_UNDEFINED_MASK;
            if (isMarginRelative()) {
                mMarginFlags |= NEED_RESOLUTION_MASK;
            } else {
                mMarginFlags &= ~NEED_RESOLUTION_MASK;
            }
        }

您需要将dp转换为像素:

Int yourDP = 250
Resources r = getResources();
float px = TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_DIP,
    yourDP,
    r.getDisplayMetrics()
);
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) linearLayoutRv.getLayoutParams();
            params.setMargins(0,0,0,(int) px); // params.setMargins(0,0,0,0); at first
            linearLayoutRv.setLayoutParams(params);

答案 2 :(得分:0)

您可以使用MotionLayout轻松地做到这一点。 只需将父项ConstraintLayout转换为MotionLayout,创建MotionScene 并在代码中使用transitionToEnd()transitionToStart()

example

答案 3 :(得分:0)

它正在工作,但以像素为单位,所以我添加了此方法:

ERROR: [HLS 214-124] use of undeclared identifier '__builtin_ia32_storeups': 

C:\Xilinx\Vivado\2019.2\win64\tools\clang\lib\clang\3.1\include\xmmintrin.h:630
ERROR: [HLS 214-124] use of undeclared identifier '__builtin_ia32_movntps'; did you mean '__builtin_ia32_movntss'?: C:\Xilinx\Vivado\2019.2\win64\tools\clang\lib\clang\3.1\include\xmmintrin.h:678
ERROR: [HLS 214-124] use of undeclared identifier '__builtin_ia32_cvtps2pd': C:\Xilinx\Vivado\2019.2\win64\tools\clang\lib\clang\3.1\include\emmintrin.h:380
ERROR: [HLS 214-124] use of undeclared identifier '__builtin_ia32_cvtdq2pd'; did you mean '__builtin_ia32_cvtdq2ps'?: C:\Xilinx\Vivado\2019.2\win64\tools\clang\lib\clang\3.1\include\emmintrin.h:386
ERROR: [HLS 214-124] use of undeclared identifier '__builtin_ia32_storeupd': C:\Xilinx\Vivado\2019.2\win64\tools\clang\lib\clang\3.1\include\emmintrin.h:586
ERROR: [HLS 214-124] use of undeclared identifier '__builtin_ia32_pavgb128': C:\Xilinx\Vivado\2019.2\win64\tools\clang\lib\clang\3.1\include\emmintrin.h:671
ERROR: [HLS 214-124] use of undeclared identifier '__builtin_ia32_pavgw128': C:\Xilinx\Vivado\2019.2\win64\tools\clang\lib\clang\3.1\include\emmintrin.h:677
ERROR: [HLS 214-124] use of undeclared identifier '__builtin_ia32_storedqu': C:\Xilinx\Vivado\2019.2\win64\tools\clang\lib\clang\3.1\include\emmintrin.h:1177
ERROR: [HLS 214-124] use of undeclared identifier '__builtin_ia32_storelv4si'; did you mean '__builtin_ia32_storelps'?: C:\Xilinx\Vivado\2019.2\win64\tools\clang\lib\clang\3.1\include\emmintrin.h:1189
ERROR: [HLS 214-124] use of undeclared identifier '__builtin_ia32_movntpd'; did you mean '__builtin_ia32_movntsd'?: C:\Xilinx\Vivado\2019.2\win64\tools\clang\lib\clang\3.1\include\emmintrin.h:1195
ERROR: [HLS 214-124] use of undeclared identifier '__builtin_ia32_movntdq'; did you mean '__builtin_ia32_movntq'?: C:\Xilinx\Vivado\2019.2\win64\tools\clang\lib\clang\3.1\include\emmintrin.h:1201
Wrong pragma usage.

并对此进行了更改:

public static int convertDpToPixel(int dp, Context context){
    return dp * ((int) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT);
}