我想要做的是在按下按钮时在“2”(第二个LinearLayout)的顶部显示“框架”(或新布局)。我该怎么办?如果没有按下按钮,请预先创建并隐藏它?
我有这种类型的布局:
XML:
<LinearLayout>
<LinearLayout>
</LinearLayout>
<LinearLayout>
//here would be another view, only shown when a button is clicked
<ViewFlipper>
</ViewFlipper>
</LinearLayout>
<RelativeLayout
</RelativeLayout>
</LinearLayout>
答案 0 :(得分:5)
使用FrameLayout
显示view
重叠另一个view
。您可以将视图保留为INVISIBLE
或在xml中使用GONE
,然后在点击Button
时将其显示。
答案 1 :(得分:2)
是的...您应该在xml中准备它并给它一个id。然后您可以使用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);
}
}
);
答案 2 :(得分:2)
这里有两个选项:
正如您所说,预先创建布局并将Visibility_Gone的可见性初始设置为布局,而不是显示,请将Visibitlity设置为View.Visible以显示布局。
另一种方法是动态创建视图,并在指定的索引上添加父级,比如添加在linearlayout之上使用:
linearLayout.addView(view, 0);
答案 3 :(得分: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);