在布局中,我有一个按钮,可以切换视图的可见性(隐藏/显示线性布局)。在这个布局里面我也有一个微调器。当我单击按钮时,它会切换可见性,但不会将布局高度更改为wrap_content,但是当我更改微调器值时,它会更新高度。
这是XML布局代码
<LinearLayout
android:id="@+id/layoutA1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvAlarmHeading"
android:layout_margin="5dip"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="@drawable/transparent_border"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/layoutA1C1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center"
android:orientation="horizontal" >
<Spinner
android:id="@+id/spnParameterA1C1"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/btnAddA1C1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:background="@drawable/button_pattern"
android:text="+"
android:textSize="22sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/layoutA1C2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center"
android:orientation="horizontal" >
<Spinner
android:id="@+id/spnConditionA1"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/btnRemoveA1C2"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:background="@drawable/button_pattern"
android:text="-"
android:textSize="22sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
以下是我切换视图可见性的活动代码
//Set the on item selected listener on the parameter spinner for alarm1 condition 1
spnParameterA1C1.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view,int position, long id)
{
tvUnitA1C1.setText(unitList.get(position));
}
@Override
public void onNothingSelected(AdapterView<?> parent){}
});
//Set onClickListener for add condition button for Alarm 1
btnAddA1C1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
layoutA1C2.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
layoutA1C2.setVisibility(android.view.View.VISIBLE);
btnAddA1C1.setVisibility(android.view.View.INVISIBLE);
layoutA1.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
}
});
//Set onClickListener for add condition button for Alarm 1
btnRemoveA1C2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
layoutA1C2.getLayoutParams().height = 0;
btnAddA1C1.setVisibility(android.view.View.VISIBLE);
layoutA1C2.setVisibility(android.view.View.INVISIBLE);
layoutA1.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
}
});
我也试过layoutA1.refreshDrawableState();
。任何人都可以告诉我应该调用哪种方法来刷新布局长度?
答案 0 :(得分:6)
您需要再次设置布局参数以刷新视图。
尝试:
LayoutParams params = layoutA1C2.getLayoutParams();
params.height = LayoutParams.WRAP_CONTENT;
layoutA1C2.setLayoutParams(params); // this call is what you need to add