3 回答

TA貢獻1831條經驗 獲得超4個贊
您不需要動態更改高度。只需將子列表放在類似于 LinearLayout 的 ViewGroup 中,View.VISIBLE
并View.GONE
在復選框更改之間切換可見性。

TA貢獻1864條經驗 獲得超2個贊
您可以使用以下代碼更改每個視圖的高度或寬度 :
private void changeIncludes (View newView ){
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) newView.getLayoutParams();
// change width and height to 50
params.height = 50 ;
params.width = 50;
newView.setLayoutParams(params);
}
或從其他視圖大小使用:
private void changeIncludes (View current , View newView ){
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) newView.getLayoutParams();
params.height = current.getLayoutParams().height;
params.width = current.getLayoutParams().width;
newView.setLayoutParams(params);
}
如果寬度或高度沒有改變,你應該刷新頁面。
這些代碼對我有用,我希望對你也有用。
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
changeIncludes(buttonView,50,100);
}else{
changeIncludes(buttonView,30,100);
}
}
});
private void changeIncludes (View newView , int h , int w){
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) newView.getLayoutParams();
// change width and height to 50
params.height = h ;
params.width = w;
newView.setLayoutParams(params);
}
我將此代碼用于復選框,但如果您想用于列表項,則只需更改偵聽器和界面。
添加回答
舉報