2017-03-03 127 views
2

我发现了很多关于Viewvisibility的问题。我已经知道.GONE和。之间的区别了。 INVISIBLE。我不知道的是如何做出适当的切换来制作它们。 VISIBLE/.GONE每当一个按钮被点击。切换视图可见性

这里是我需要的:我有一个linear layout与一些buttons里面。我需要他们buttons隐藏在首位,所以我设置linear layout为不见了:

<LinearLayout 
    android:id="@+id/feelings_layout" 
    android:layout_below="@+id/feeling_btn" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:visibility="gone"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
     <Button 
      android:id="@+id/happy_btn" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="46dp" 
      android:text="Happy"/> 
     <Button 
      android:id="@+id/sad_btn" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="46dp" 
      android:text="Sad"/> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
     <Button 
      android:id="@+id/love_btn" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="46dp" 
      android:text="in love"/> 
     <Button 
      android:id="@+id/mad_btn" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="46dp" 
      android:text="mad"/> 
    </LinearLayout> 

</LinearLayout> 

然后我make'em。当点击一个按钮时,会出现VISIBLEGONE当再次相同button按:

Button feelingsButton = (Button)contentView.findViewById(R.id.feeling_btn); 
    feelingsButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast.makeText(context, "button clicked", Toast.LENGTH_LONG).show(); 
      feelingsButtonsLayout = (LinearLayout)contentView.findViewById(R.id.feelings_layout); 
      if(feelingsButtonsLayout.getVisibility() == View.GONE){ 
       Log.d("-----------", "gone"); 
       feelingsButtonsLayout.setVisibility(View.VISIBLE); 

      }else{ 
       Log.d("-----------", "not gone"); 
       for (int i = 0; i < feelingsButtonsLayout.getChildCount(); i++){ 
        View view = feelingsButtonsLayout.getChildAt(i); 
        view.setVisibility(View.GONE); 
       } 
       feelingsButtonsLayout.setVisibility(View.GONE); 
      } 
     } 
    }); 

一切似乎都做工精细,但是当我点击同一按钮第三次,期待它使布局VISIBLE,就不会再显示即使我的log表示该视图是gone(只看到Logcat,它似乎工作正常)。

对此的任何想法?

回答

2

您必须重新设置所有按钮的知名度将其在第二次点击设置为GONE因为

第1点击=>设置您的布局可见,所有按钮都已经可见

第2点击=>组布局消失以及所有按钮

3点击=>设置布局可见,但按钮依然不可见其分别设置了2号的同时点击

if(feelingsButtonsLayout.getVisibility() == View.GONE){ 
       Log.d("-----------", "gone"); 
       for (int i = 0; i < feelingsButtonsLayout.getChildCount(); i++){ 
        View view = feelingsButtonsLayout.getChildAt(i); 
        view.setVisibility(View.VISIBLE); 
       } 
       feelingsButtonsLayout.setVisibility(View.VISIBLE); 

      }else{ 
       Log.d("-----------", "not gone"); 
       for (int i = 0; i < feelingsButtonsLayout.getChildCount(); i++){ 
        View view = feelingsButtonsLayout.getChildAt(i); 
        view.setVisibility(View.GONE); 
       } 
       feelingsButtonsLayout.setVisibility(View.GONE); 
      } 

你可以删除这两个循环和简单地设置您的container布局知名度

if(feelingsButtonsLayout.getVisibility() == View.GONE){ 
       Log.d("-----------", "gone"); 
       feelingsButtonsLayout.setVisibility(View.VISIBLE); 

      }else{ 
       Log.d("-----------", "not gone"); 
       feelingsButtonsLayout.setVisibility(View.GONE); 
      } 
+0

感谢的人,这是一个很好的解释。我刚刚删除了循环,它都工作得很好。我会将上面的答案标记为已接受的答案,不过因为它也解决了我的问题,他首先回答好吗?干杯! – Rob

+0

@rob检查先回答的时间,但解释和回答都需要时间,无论如何,我很高兴我可以帮助 –

+0

该死的,对!我的坏人! – Rob

1

的问题是要设置GONE所有的各个按钮,但在布局上的按钮单独,而不是设置VISIBLE

当父配置设置为GONE时,不需要隐藏所有按钮。您可以从您的else中删除下面的代码

for (int i = 0; i < feelingsButtonsLayout.getChildCount(); i++){ 
       View view = feelingsButtonsLayout.getChildAt(i); 
       view.setVisibility(View.GONE); 
      } 
+0

谢谢你,解决了我的问题! – Rob