开发者

Hiding linear layout on runtime in Android

I am having following layout

<merge>
    <LinearLayout
        android:id="@+id/ll_main"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        />
    <LinearLayout
        android:id="@+id/ll_sub"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        />  
</merge>

What I want to do is to show/hide the ll_sub layout on runtime through setVisibility() but it is not working.

When I am setting android:visibility="gone" (also I had checked with 开发者_运维问答invisible) from the xml of ll_sub then it is not displayed on the screen and this time when I use setVisibility() to show this layout on runtime, it is displayed but when I try to hide this layout once it is displayed then it is not hiding.

EDIT

I am trying to show/hide this linear layout on click of a button.

LinearLayout ll;
Button minimize;
int visibility=0;

@Override
public void onCreate(Bundle savedInstanceState)
{
    ll=(LinearLayout)findViewById(R.id.ll_sub);
    minimize=(Button)findViewById(R.id.minimize);
    minimize.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View view)
        {

            if(visibility==0)
            {
                visibility=2;
            }
            else
            {
                visibility=0;
            }
            ll.setVisibility(visibility);

        }
    });
}


It looks like you're setting the wrong constants for changing view visibility.

GONE == 8
INVISIBLE == 4
VISIBLE == 0

However, you should never rely on the actual values that Android happened to designate to represent their constants. Instead use the the values defined in the View class: View.VISIBLE, View.INVISIBLE, and View.GONE.

// snip...
if(visibility == View.VISIBLE)
{
    visibility = View.GONE;
}
else
{
    visibility = View.VISIBLE;
}
ll.setVisibility(visibility);

And don't forget to call invalidate() on the view :)


You should use the Constants provided by View

View.INVISBLE, View.VISIBLE, View.GONE

and also invalidate your View

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜