2017-03-03 18 views
2

Viewvisibilityについて多くの質問がありました。私はすでに.GONEと.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。 VISIBLEボタンをクリックすると再び GONE同じ 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を作るために期待し、同じボタン3回目をクリックしたときに、それもかかわらず、再び表示されません私のlogは、表示がgoneであると言います(Logcatのみを見ると、正常に動作しているようです)。

これに関するご意見はありますか?あなたは

第一クリックが=>あなたが目に見えるレイアウトを設定し、すべてのボタンため、2番目のクリックでGONEに設定されたもう一度、すべてのボタンの可視性を設定する必要が

答えて

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); 
      } 
012しばらく

または

あなたがループの両方を削除し、単にあなたのcontainerレイアウトの可視性

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

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

ありがとう、それは素晴らしい説明です。私はループを削除したばかりで、うまくいきました。私は問題を解決し、彼は最初にokに答えたので、私は受け入れられたものとして上記の答えをマークしますか?乾杯! – 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