【发布时间】:2017-07-23 14:53:58
【问题描述】:
我发现了很多关于 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>
然后我在单击按钮时将其设为 .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