android - 如何在 Android 中显示/隐藏分组 View ?

标签 android android-relativelayout visibility android-view show-hide

我想创建一个 Activity ,如照片中提到的... 一旦我按下最大化按钮,我希望它变成 Activity 的全屏,第 1 部分变成最小化,当我再次按下恢复按钮时,我希望它变成第一种状态:能够看到第 1 部分和第 2 部分 ...

我认为如果我们放置两个布局是可能的吗?不是吗?请向我推荐可帮助我解决此问题的资源,或向我展示实现解决方案的代码。

enter image description here

最佳答案

第一部分和第二部分应该有自己的布局。之后,使用每个布局的 visilibity 属性。具体来说,要隐藏任何 View 而不继续占用其空间,请为可见性属性使用值 gone

好的,我来了。下面是如何隐藏/显示分组 View 的完整示例。

ma​​in.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/

相关文章:

android - 自定义 View 的 onMeasure : how to get width based on height

build - 如何将熔岩灯/环境球连接到我的构建中?

android - 如何知道在android中加载布局需要多少时间?

android - 在 Android 中以编程方式在相对布局中添加多个相对布局

java - JButton 仅在可见性设置为 True-Java 时才起作用

javascript - 使用 jQuery : How can a value be updated to match an elements visibility?

android - 使用 Amazon Web Services (AWS) 简单通知服务 (SNS) 设置 Google Cloud Messenging (GCM)

java - WebView 页面不可用 - “Upi://”

android - 在android中立即获取位置

android - 使用 Jitsi Sdk 和 Quickblox-android-sdk 时重复类 org.webrtc.AndroidVideoDecoder