2012-02-10 27 views
1

我想要做的是在按下按钮时在“2”(第二个LinearLayout)的顶部显示“框架”(或新布局)。我应该怎么做?如果按钮没有按下,预先制作并使其隐藏起来?Android:将东西放在线性布局上

我有这种类型的布局:

layout

XML:

<LinearLayout> 

    <LinearLayout> 
    </LinearLayout> 

    <LinearLayout> 
     //here would be another view, only shown when a button is clicked 
     <ViewFlipper> 
     </ViewFlipper> 
    </LinearLayout> 

    <RelativeLayout  
    </RelativeLayout> 

</LinearLayout> 
+1

尝试隐藏和显示。 – 2012-02-10 09:33:26

回答

1

如果你想显示按钮点击任何视图,则首先把那个查看XML内部并使其可见性消失,并在按钮单击使其可见。我已经把你的代码中的imageview设置为可见,因此它不会在布局中显示。

<LinearLayout> 

    <LinearLayout> 
    </LinearLayout> 

    <LinearLayout> 
     //here would be another view, only shown when a button is clicked 
     <ImageView 
         android:id="@+id/image1" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:src="@drawable/icon" 
         android:visibility="gone" /> 
    </LinearLayout> 

    <RelativeLayout  
    </RelativeLayout> 

</LinearLayout> 

为了使可见图像视图,

imag1.seVisibility(View.VISIBLE); 
+1

不错的答案chirag ... – Android 2012-02-10 09:44:30

+0

不错的一个.......... – 2012-02-10 10:10:19

+0

谢谢你,会这样做。 – 2012-02-10 10:46:53

2

是...你应该在XML准备它,并给它一个id.then你可以轻松管理其按钮点击的可见性使用mLinearLayout.setVisibility(View.GONE);mLinearLayout.setVisibility(View.VISIBLE);像:

Button mButton=(Button)findViewById(R.id.button); 
LinearLayout ll=(LinearLayout)findViewById(R.id.frame_layout); 

static int count=0; 
mButton.setOnClick.... (new OnClick...() 

      public void onClick(){ 

       count++; 
       if(count==1)    
        ll.setVisibility(View.VISIBLE);    
       else 
       { 
        count=0; 
        ll.setVisibility(View.GONE); 
       } 
      }   
); 
+0

我想代替'if(count == 1)'..我们可以使用'if(ll.isShown())' – MKJParekh 2012-02-10 09:45:41

+0

我还没有尝试过它。可能是其他方式做到这一点! – Hiral 2012-02-10 09:46:34

+0

没问题,只是看到额外使用静态变量..所提到的.. – MKJParekh 2012-02-10 09:51:14

5

使用FrameLayout显示view重叠另一个view。您可以将视图保留为INVISIBLE或在xml中使用GONE,然后在点击Button时使其可见。

+0

不错的答案lalit ... – Android 2012-02-10 09:46:37

2

在这里,你有两个选择:

正如你所说的预创建的布局和设置能见度Visibility_Gone最初的布局,不显示,设置Visibitlity到View.Visible显示布局。

另一种方法是动态创建视图,并添加到指定索引的父母,喜欢在LinearLayout中使用的顶部添加:

linearLayout.addView(view, 0); 
+0

谢谢你的回答。 – 2012-02-10 10:50:00