Android - 如果两个列表视图都打开则共享 space,否则使用所有 space
Android - Have two listviews share space if both open, else use all the space
我有一个带有列表视图的按钮。 XML 看起来像这样:
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:text="@string/button1_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
按下按钮显示或隐藏其下方的列表视图,使用此代码:
private OnClickListener expandOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (listview1.getVisibility() == View.VISIBLE) {
listview1.setVisibility(View.GONE);
} else {
listview1.setVisibility(View.VISIBLE);
}
}
};
结果如下所示:
现在,我有两个 button/listview 组合。
如果两者都打开,他们应该在屏幕上共享 space。
如果一个或另一个打开,它们应该尽可能多地占据屏幕,而不是将按钮推离屏幕。
我试过使用重量。我尝试将线性布局的高度操纵为 'wrap content' 或专门计算的像素值。我试图通过它进行调试。它似乎命中了代码中的所有正确位置,但 UI 的行为不稳定。关于如何做到这一点有什么想法吗?
写完我的整个问题后,我决定尝试一些事情,而且令人惊讶的是它有效。它是设置权重和高度的组合。
XML:
<LinearLayout
android:id="@+id/llWrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/llOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button1" />
<ListView
android:id="@+id/listview1"
style="@style/text.body"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/llTwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button2" />
<ListView
android:id="@+id/listview2"
style="@style/text.body"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Java代码:
private OnClickListener expandOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
toggleONEVisibility();
break;
case R.id.button2:
toggleTWOVisibility();
break;
}
}
};
private void toggleONEVisibility() {
LinearLayout.LayoutParams layoutParam2 = (LinearLayout.LayoutParams)linearLayout2.getLayoutParams();
LinearLayout.LayoutParams layoutParam1 = (LinearLayout.LayoutParams)linearLayout1.getLayoutParams();
// if ONE is visible
// if TWO is visible
// set TWO to 100%
// set ONE to GONE
// else ONE is NOT visible
// if TWO is visible
// set ONE to 50%
// set TWO to 50%
// else TWO is NOT visible
// set ONE to 100%
// set ONE to visible
if (listview1.getVisibility() == View.VISIBLE) {
listview1.setVisibility(View.GONE);
layoutParam1.weight = 0f;
layoutParam1.height = LayoutParams.WRAP_CONTENT;
if (listview2.getVisibility() == View.VISIBLE) {
layoutParam2.weight = 1f;
layoutParam2.height = 0;
}
} else {
listview1.setVisibility(View.VISIBLE);
if (listview2.getVisibility() == View.VISIBLE) {
layoutParam1.weight = 50f;
layoutParam1.height = 0;
layoutParam2.weight = 50f;
layoutParam2.height = 0;
} else {
layoutParam1.weight = 1f;
layoutParam1.height = 0;
layoutParam2.weight = 0f;
layoutParam2.height = LayoutParams.WRAP_CONTENT;
}
}
linearLayout1.setLayoutParams(layoutParam1);
linearLayout2.setLayoutParams(layoutParam2);
}
private void toggleTWOVisibility() {
LinearLayout.LayoutParams layoutParam2 = (LinearLayout.LayoutParams)linearLayout2.getLayoutParams();
LinearLayout.LayoutParams layoutParam1 = (LinearLayout.LayoutParams)linearLayout1.getLayoutParams();
// if TWO is visible
// if ONE is visible
// set ONE to 100%
// set TWO to GONE
// else TWO is NOT visible
// if ONE is visible
// set ONE to 50%
// set TWO to 50%
// else ONE is NOT visible
// set TWO to 100%
// set TWO to visible
if (listview2.getVisibility() == View.VISIBLE) {
listview2.setVisibility(View.GONE);
layoutParam2.weight = 0f;
layoutParam2.height = LayoutParams.WRAP_CONTENT;
if (listview1.getVisibility() == View.VISIBLE) {
layoutParam1.weight = 1f;
layoutParam1.height = 0;
}
} else {
listview2.setVisibility(View.VISIBLE);
if (listview1.getVisibility() == View.VISIBLE) {
layoutParam1.weight = 50f;
layoutParam1.height = 0;
layoutParam2.weight = 50f;
layoutParam2.height = 0;
} else {
layoutParam1.height = LayoutParams.WRAP_CONTENT;
layoutParam1.weight = 0f;
layoutParam2.weight = 1f;
layoutParam2.height = 0;
}
}
linearLayout1.setLayoutParams(layoutParam1);
linearLayout2.setLayoutParams(layoutParam2);
}
我有一个带有列表视图的按钮。 XML 看起来像这样:
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:text="@string/button1_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
按下按钮显示或隐藏其下方的列表视图,使用此代码:
private OnClickListener expandOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (listview1.getVisibility() == View.VISIBLE) {
listview1.setVisibility(View.GONE);
} else {
listview1.setVisibility(View.VISIBLE);
}
}
};
结果如下所示:
现在,我有两个 button/listview 组合。
如果两者都打开,他们应该在屏幕上共享 space。
如果一个或另一个打开,它们应该尽可能多地占据屏幕,而不是将按钮推离屏幕。
我试过使用重量。我尝试将线性布局的高度操纵为 'wrap content' 或专门计算的像素值。我试图通过它进行调试。它似乎命中了代码中的所有正确位置,但 UI 的行为不稳定。关于如何做到这一点有什么想法吗?
写完我的整个问题后,我决定尝试一些事情,而且令人惊讶的是它有效。它是设置权重和高度的组合。
XML:
<LinearLayout
android:id="@+id/llWrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/llOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button1" />
<ListView
android:id="@+id/listview1"
style="@style/text.body"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/llTwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button2" />
<ListView
android:id="@+id/listview2"
style="@style/text.body"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Java代码:
private OnClickListener expandOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
toggleONEVisibility();
break;
case R.id.button2:
toggleTWOVisibility();
break;
}
}
};
private void toggleONEVisibility() {
LinearLayout.LayoutParams layoutParam2 = (LinearLayout.LayoutParams)linearLayout2.getLayoutParams();
LinearLayout.LayoutParams layoutParam1 = (LinearLayout.LayoutParams)linearLayout1.getLayoutParams();
// if ONE is visible
// if TWO is visible
// set TWO to 100%
// set ONE to GONE
// else ONE is NOT visible
// if TWO is visible
// set ONE to 50%
// set TWO to 50%
// else TWO is NOT visible
// set ONE to 100%
// set ONE to visible
if (listview1.getVisibility() == View.VISIBLE) {
listview1.setVisibility(View.GONE);
layoutParam1.weight = 0f;
layoutParam1.height = LayoutParams.WRAP_CONTENT;
if (listview2.getVisibility() == View.VISIBLE) {
layoutParam2.weight = 1f;
layoutParam2.height = 0;
}
} else {
listview1.setVisibility(View.VISIBLE);
if (listview2.getVisibility() == View.VISIBLE) {
layoutParam1.weight = 50f;
layoutParam1.height = 0;
layoutParam2.weight = 50f;
layoutParam2.height = 0;
} else {
layoutParam1.weight = 1f;
layoutParam1.height = 0;
layoutParam2.weight = 0f;
layoutParam2.height = LayoutParams.WRAP_CONTENT;
}
}
linearLayout1.setLayoutParams(layoutParam1);
linearLayout2.setLayoutParams(layoutParam2);
}
private void toggleTWOVisibility() {
LinearLayout.LayoutParams layoutParam2 = (LinearLayout.LayoutParams)linearLayout2.getLayoutParams();
LinearLayout.LayoutParams layoutParam1 = (LinearLayout.LayoutParams)linearLayout1.getLayoutParams();
// if TWO is visible
// if ONE is visible
// set ONE to 100%
// set TWO to GONE
// else TWO is NOT visible
// if ONE is visible
// set ONE to 50%
// set TWO to 50%
// else ONE is NOT visible
// set TWO to 100%
// set TWO to visible
if (listview2.getVisibility() == View.VISIBLE) {
listview2.setVisibility(View.GONE);
layoutParam2.weight = 0f;
layoutParam2.height = LayoutParams.WRAP_CONTENT;
if (listview1.getVisibility() == View.VISIBLE) {
layoutParam1.weight = 1f;
layoutParam1.height = 0;
}
} else {
listview2.setVisibility(View.VISIBLE);
if (listview1.getVisibility() == View.VISIBLE) {
layoutParam1.weight = 50f;
layoutParam1.height = 0;
layoutParam2.weight = 50f;
layoutParam2.height = 0;
} else {
layoutParam1.height = LayoutParams.WRAP_CONTENT;
layoutParam1.weight = 0f;
layoutParam2.weight = 1f;
layoutParam2.height = 0;
}
}
linearLayout1.setLayoutParams(layoutParam1);
linearLayout2.setLayoutParams(layoutParam2);
}