【问题标题】:Toggle View VisibilityToggle 查看可见性
【发布时间】:2017-07-23 14:53:58
【问题描述】:

我发现了很多关于 View visibility 的问题。我已经知道 .GON​​E 和 .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>

然后我在单击按钮时将其设为 .VISIBLE,并在按下相同的 button 时再次设为 .GONE

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,它似乎工作正常)。

对此有什么想法吗?

【问题讨论】:

    标签: android view toggle visibility


    【解决方案1】:

    问题是您将GONE 设置为所有单独的按钮,但将VISIBLE 设置为单独的布局而不是按钮。

    当父布局设置为GONE 时,无需隐藏所有按钮。您可以从 else 案例中删除以下代码

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

    【讨论】:

      【解决方案2】:

      您必须再次设置所有按钮的可见性,这些按钮在第二次点击时设置为GONE,因为

      第一次点击 => 设置你的布局可见并且所有按钮都已经可见

      第二次点击 => 设置布局以及所有按钮

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

      【讨论】:

      • 谢谢,这是一个很好的解释。我刚刚删除了循环,一切都很好。我会将上面的答案标记为已接受的答案,因为它也解决了我的问题,他首先回答了好吗?干杯!
      • @rob 检查第一个回答的时间,虽然解释和回答都需要时间,无论如何我很高兴我能提供帮助
      • @rob 你可以给像 arun 这样有用的答案投票,以感谢他的努力,快乐编码
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-20
      • 2018-12-11
      • 2018-07-18
      • 2013-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多