在运行Android中隐藏的线性布局 [英] Hiding linear layout on runtime in Android
问题描述
我有以下布局
<合并>
<的LinearLayout
机器人:ID =@ + ID / ll_main
机器人:layout_height =FILL_PARENT
机器人:layout_width =FILL_PARENT
/>
<的LinearLayout
机器人:ID =@ + ID / ll_sub
机器人:layout_height =FILL_PARENT
机器人:layout_width =FILL_PARENT
/>
< /合并>
我想要做的是显示/通过 setVisibility隐藏在运行时ll_sub布局()
,但它不能正常工作。
当我设置安卓知名度=水涨船高
(也我曾与检查无形
)从XML ll_sub
那么它不显示在屏幕上,当我利用这段时间 setVisibility()
来显示这个布局在运行时,它会显示,但是当我试图掩盖这种布局一旦则显示它不躲了起来。
修改
我想显示/隐藏此上点击一个按钮线性布局。
的LinearLayout LL;
按钮减少;
INT能见度= 0;
@覆盖
公共无效的onCreate(包savedInstanceState)
{
LL =(的LinearLayout)findViewById(R.id.ll_sub);
最大限度地减少=(按钮)findViewById(R.id.minimize);
minimize.setOnClickListener(新View.OnClickListener()
{
公共无效的onClick(视图查看)
{
如果(能见度== 0)
{
能见度= 2;
}
其他
{
能见度= 0;
}
ll.setVisibility(知名度);
}
});
}
它看起来像你设置了错误的常数改变的视图知名度。
GONE == 8
隐形== 4
可见== 0
不过,你不应该依赖于机器人的发生的实际值的指定重新present的常数。而是使用在视图类中定义的值: View.VISIBLE
, View.INVISIBLE
和 View.GONE
。
//略...
如果(能见度== View.VISIBLE)
{
可见= View.GONE;
}
其他
{
可见= View.VISIBLE;
}
ll.setVisibility(知名度);
不要忘记调用无效()
的观点:)
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 :)
这篇关于在运行Android中隐藏的线性布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!