How to show/hide grouped views in Android?

前端 未结 2 1348
无人共我
无人共我 2021-02-08 20:55

I want to create an activity such as mentioned in photo... as soon as I press the maximize button I want it to become full screen for the activity and part 1 become minimize, an

2条回答
  •  独厮守ぢ
    2021-02-08 21:43

    Part one and two should be in their own layout. After, play with the visilibity property of each layout. Specifically to hide any view without it continues to occupy its space, use the value gone for the visibility property.

    Ok, here I go. Below you have a complete example of how to hide/show grouped views.

    main.xml

    
    
        
    
            
    
            
    
            
        
    
        

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

    I hope this helps ;)

提交回复
热议问题