【问题标题】:How to show/hide grouped views in Android?如何在 Android 中显示/隐藏分组视图?
【发布时间】:2013-09-22 01:02:45
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    第一部分和第二部分应该有自己的布局。之后,使用每个布局的 visilibity 属性。特别是要隐藏任何视图而不使其继续占用其空间,请将值 gone 用于可见性属性。

    好的,我去。下面是如何隐藏/显示分组视图的完整示例。

    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>
    

    活动

    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;
    }
    

    我希望这会有所帮助;)

    【讨论】:

    • 如何将一些小部件放入单个组中以提供将所有子小部件隐藏在一起的能力?例如:在 vb.net 中,我们可以将面板或 groupbox 作为容器,我们可以使其不可见,并且面板或 groupbox 内的所有项目都变得不可见......我在 android 中需要这样的东西......谢谢
    • 我只是编辑了我之前的答案。看我制作的示例代码。
    • 不客气。如果答案有帮助,请标记为已接受答案;)
    【解决方案2】:

    有一个比 Diego Palomar 产生的更简单的解决方案,没有使用额外的变量。我拿他的代码来展示一下:

        public class MyActivity extends Activity implements OnClickListener { 
    
        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 (mViewGroup.getVisibility() == View.VISIBLE) {
            mViewGroup.setVisibility(View.GONE);
            mButton.setText("Show");
        } else {
            mViewGroup.setVisibility(View.VISIBLE);
            mButton.setText("Hide");
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多