防止视图在旋转后显示

时间:2017-07-15 19:39:37

标签: android android-layout android-view android-constraintlayout

我在另一个约束布局上面添加了一个约束布局,在它内部有一个按钮,当我点击它时它应该使约束布局和按钮消失 这是代码

    Button button11 = (Button) findViewById(R.id.button11);
        final ConstraintLayout constraintLayout = (ConstraintLayout) findViewById(R.id.constraint_layout_inside);
        button11.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                constraintLayout.setVisibility(View.INVISIBLE);
}

当我旋转屏幕时,问题再次出现 怎么预防呢?

2 个答案:

答案 0 :(得分:1)

无法自动处理,因为活动在方向更改后被销毁并创建。因此,你必须自己处理它。

onSaveInstanceState()中保存当前状态:


    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        outState.putBoolean("someKey", constraintLayout.visibility == View.VISIBLE)        
    }

然后,在onCreate()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Initialize views
        ...

        // if `savedInstanceState` is not null, then look for `someKey` in bundle
        savedInstanceState?.run {
            val isVisible = getBoolean("someKey")
            constraintLayout.visibility = if (isVisible) View.VISIBLE else View.INVISIBLE
        } 

        if (null == savedInstanceState) {
          // This activity is starting for the first time, 
          // not an orientation change case
        }
    }

答案 1 :(得分:-1)

将此方法添加到您的活动

        <!-- Custom info markers -->

     <div id="map_show" class="" style=""></div>

  <script type="text/javascript">

  var InfoBoxBuilder, handler,
      extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
      hasProp = {}.hasOwnProperty;

    InfoBoxBuilder = (function(superClass) {
      extend(InfoBoxBuilder, superClass);

      function InfoBoxBuilder() {
        return InfoBoxBuilder.__super__.constructor.apply(this, arguments);
      }

      InfoBoxBuilder.prototype.create_infowindow = function() {
        var boxText;
        if (!_.isString(this.args.infowindow)) {
          return null;
        }
        boxText = document.createElement("div");
        boxText.setAttribute('class', 'yourClass'); // This is where you add a class style it in your css and see what happens 
        boxText.innerHTML = this.args.infowindow;
        return this.infowindow = new InfoBox(this.infobox(boxText));
      };

      InfoBoxBuilder.prototype.infobox = function(boxText) {
        return {
          content: boxText,
          pixelOffset: new google.maps.Size(-140, 0),
          boxStyle: {
            width: "280px"
          }
        };
      };

      return InfoBoxBuilder;

    })(Gmaps.Google.Builders.Marker);

        handler = Gmaps.build('Google');
        handler.buildMap({ provider: {}, internal: {id: 'map_show'}}, function() {
          markers = handler.addMarkers(<%=raw @hash.to_json %>);
          handler.bounds.extendWith(markers);
          handler.fitMapToBounds();


        });
    </script>

你的清单中的这一行

 boxText = document.createElement("div");
    boxText.setAttribute('class', 'yourClass'); // This is where you add a class style it in your css and see what happens