我想为framelayout设置高度和宽度,如果使用setlayoutparams给java代码中的值显示非常小的框,但在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>
在xml中给出的值,以便正确显示: Result using XML
给定java代码中的值,因此它显示的非常小 Result using Java
如何以编程方式更改framelayout宽度和高度?
请帮帮我。
答案 0 :(得分:0)
试试这个
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);
答案 1 :(得分:0)
使用此
FrameLayout frameLayout2= (FrameLayout) light.findViewById(R.id.frameLayout2);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(300, 300);
frameLayout2.setLayoutParams(lp);