我想创建一个 Activity ,如照片中提到的... 一旦我按下最大化按钮,我希望它变成 Activity 的全屏,第 1 部分变成最小化,当我再次按下恢复按钮时,我希望它变成第一种状态:能够看到第 1 部分和第 2 部分 ...
我认为如果我们放置两个布局是可能的吗?不是吗?请向我推荐可帮助我解决此问题的资源,或向我展示实现解决方案的代码。
最佳答案
第一部分和第二部分应该有自己的布局。之后,使用每个布局的 visilibity
属性。具体来说,要隐藏任何 View 而不继续占用其空间,请为可见性属性使用值 gone
。
好的,我来了。下面是如何隐藏/显示分组 View 的完整示例。
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/viewsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextBox One" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="TextBox Two" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="TextBox Three" />
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Hide" />
</RelativeLayout>
Activity
public class MyActivity extends Activity implements OnClickListener {
private boolean viewGroupIsVisible = true;
private View mViewGroup;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mViewGroup = findViewById(R.id.viewsContainer);
mButton = findViewById(R.id.button);
mButton.setOnClickListener(this);
}
@Override
public void onClick(View button) {
if (viewGroupIsVisible) {
mViewGroup.setVisibility(View.GONE);
mButton.setText("Show");
} else {
mViewGroup.setVisibility(View.VISIBLE);
mButton.setText("Hide");
}
viewGroupIsVisible = !viewGroupIsVisible;
}
我希望这对您有所帮助;)
关于android - 如何在 Android 中显示/隐藏分组 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18852423/