Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中通过编程设置framelayout的高度和宽度_Android_Android Framelayout - Fatal编程技术网

如何在android中通过编程设置framelayout的高度和宽度

如何在android中通过编程设置framelayout的高度和宽度,android,android-framelayout,Android,Android Framelayout,我想为framelayout设置高度和宽度,如果在java代码中使用setlayoutparams指定值,它会显示非常小的框,但在XML中它会正确显示 这是我的java代码 FrameLayout frameLayout2= (FrameLayout) light.findViewById(R.id.frameLayout2); ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(300, 300); f

我想为framelayout设置高度和宽度,如果在java代码中使用setlayoutparams指定值,它会显示非常小的框,但在XML中它会正确显示

这是我的java代码

 FrameLayout frameLayout2= (FrameLayout) light.findViewById(R.id.frameLayout2);
 ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(300, 300);
 frameLayout2.setLayoutParams(lp);
这是我的xml: 注意:约束布局是我的父项

<android.support.constraint.ConstraintLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LightPlaceFragment"
    tools:layout_editor_absoluteY="81dp"
    tools:layout_editor_absoluteX="0dp"
    android:background="@color/colorAccent">



    <FrameLayout
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="0.882"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/l_width"
        app:layout_constraintVertical_bias="0.162"
        android:id="@+id/frameLayout2">


        <include
            android:id="@+id/other"
            android:layout_width="300dp"
            android:layout_height="200dp"
            android:visibility="visible"
            layout="@layout/sample1" />

    </FrameLayout>



</android.support.constraint.ConstraintLayout>

给定java代码中的值,因此它显示的框非常小

如何通过编程更改框架布局的宽度和高度

请帮帮我。

试试这个

FrameLayout frameLayout2= (FrameLayout) light.findViewById(R.id.frameLayout2);
ConstraintLayout.LayoutParams oldParams = (ConstraintLayout.LayoutParams) frameLayout2.getLayoutParams();
oldParams.width=400;
oldParams.height=500;
frameLayout2.setLayoutParams(oldParams);
还是这个

FrameLayout frameLayout2= (FrameLayout) light.findViewById(R.id.frameLayout2);
ConstraintLayout.LayoutParams oldParams= new ConstraintLayout.LayoutParams(300,300);
frameLayout2.setLayoutParams(oldParams);
用这个

 FrameLayout frameLayout2= (FrameLayout) light.findViewById(R.id.frameLayout2);
 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(300, 300);
 frameLayout2.setLayoutParams(lp);

是的,谢谢,但我以编程的方式给出了值,它没有正确地显示,小框只显示,我在xml中给出的相同值意味着显示properly@KalaivaniR根据您的要求设置高度。在xml中,我给出300*300正确显示大方框,但如果我在java代码中给出相同的300*300,则在右侧显示非常小的方框。我编辑了问题和附加的屏幕截图,请查看并给出解决方案