我发现了很多关于View
visibility
的问题。我已经知道.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
,就不会再显示即使我的log
表示该视图是gone
(只看到Logcat,它似乎工作正常)。
对此的任何想法?
感谢的人,这是一个很好的解释。我刚刚删除了循环,它都工作得很好。我会将上面的答案标记为已接受的答案,不过因为它也解决了我的问题,他首先回答好吗?干杯! – Rob
@rob检查先回答的时间,但解释和回答都需要时间,无论如何,我很高兴我可以帮助 –
该死的,对!我的坏人! – Rob