热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

每次单击菜单时都会切换linearlayout(imageview)-Toggleoflinearlayoutoneveryclickofmenu(imageview)

Ihaveamenuimageview,whenclickedonitmylinearlayout(line2)appears,butitshouldagaindisa

I have a menu imageview, when clicked on it my linearlayout(line2) appears, but it should again disappear on the next click of menu imageview

我有一个菜单imageview,当点击它时我会出现linearlayout(line2),但它会在下一次菜单imageview上再次消失

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
line1=(LinearLayout) findViewById(R.id.ll1);
         line2=(LinearLayout)findViewById(R.id.ll2);

         menu=(ImageView)findViewById(R.id.menu);

         menu.setOnClickListener(new OnClickListener() {

       public void onClick(View v) {
           menu.setVisibility(View.VISIBLE);
         // TODO Auto-generated method stub
       line1.setVisibility(View.VISIBLE);
       line2.setVisibility(View.VISIBLE);
         }
     });

2 个解决方案

#1


0  

You can use an if/else statement to check the current state every time your button is pressed and switch. You don't necessarily need to check for the visibility being GONE if you're not going to set it to GONE ever so you can remove that condition.

每次按下按钮并切换时,都可以使用if / else语句检查当前状态。如果您不打算将其设置为GONE,则不一定需要检查GONE的可见性,以便您可以删除该条件。

The difference between GONE and INVISIBLE is that the layout reacts to gone as if it weren't even there, the element no longer causes any displacement.

GONE和INVISIBLE之间的区别在于布局的反应就像它甚至不存在一样,元素不再导致任何位移。

Change line2.setVisibility(View.VISIBLE); to

更改line2.setVisibility(View.VISIBLE);至

   if (line2.getVisibility() == View.GONE || line2.getVisibility() == View.INVISIBLE ) {
       line2.setVisibility(View.VISIBLE); } 
   else { 
       line2.setVisibility(View.GONE); // Set it to View.INVISIBLE if thats your goal.
   }

#2


1  

You can try -

你可以试试 -

       //line2.setVisibility(View.VISIBLE);
       if(line2.getVisibility() == View.VISIBLE)
       {
           line2.setVisibility(View.GONE);
       }
       else
       {
           line2.setVisibility(View.VISIBLE);
       }

推荐阅读
author-avatar
多米音乐_35692689
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有