问题描述
我的主布局 main.xml 只包含两个 LinearLayout:
My main layout main.xml simply contains two LinearLayouts:
- 第一个
LinearLayout
承载一个VideoView
和一个Button
, - 第二个
LinearLayout
承载了一个EditText
,并且这个LinearLayout
已将 visibility 值设置为GONE" (android:visibility="gone"
)
- The 1st
LinearLayout
hosts aVideoView
and aButton
, - The 2nd
LinearLayout
hosts anEditText
, and thisLinearLayout
has set the visibility value to "GONE" (android:visibility="gone"
)
如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/first_ll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<VideoView
android:id="@+id/my_video"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="9"
/>
<Button
android:id="@+id/my_btn"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_gravity="right|bottom"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/second_ll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="2dip"
android:visibility="gone"
>
<EditText
android:id="@+id/edit_text_field"
android:layout_height="40dip"
android:layout_width="fill_parent"
android:layout_weight="5"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
</LinearLayout>
我成功实现了当 Button
(id my_btn)被按下时,2nd LinearLayout
和 EditText 字段,其中包含以下 Java 代码:
I successfully implemented the feature that when the
Button
(with id my_btn) is pressed, the 2nd LinearLayout
with EditText
field is shown, with the following Java code:
LinearLayout secondLL = (LinearLayout) findViewById(R.id.second_ll);
Button myBtn = (Button) findViewById(R.id.my_btn);
myBtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
int visibility = secondLL.getVisibility();
if(visibility==View.GONE)
secondLL.setVisibility(View.VISIBLE);
}
});
使用上面的 Java 代码,2nd
LinearLayout
和 EditText
显示为 附加在下面 >1st LinearLayout
这是有道理的.
With the above Java code, the 2nd
LinearLayout
with EditText
is shown like appending below the 1st LinearLayout
which makes sense.
但是,我需要的是:当
Button
(id: my_btn) 被按下时,2nd LinearLayout
with EditText
显示在1st LinearLayout
的顶部,看起来像2nd LinearLayout
和 EditText
从屏幕底部上升,2nd LinearLayout
和 EditText
只占屏幕底部的一部分,即第一个 LinearLayout 仍然可见,如下图所示:
BUT, What I need is: when
Button
(id: my_btn) is pressed, the 2nd LinearLayout
with EditText
is shown on top of the 1st LinearLayout
, which looks like the 2nd LinearLayout
with EditText
is rising from the bottom of screen, and the 2nd LinearLayout
with EditText
only occupy part of the screen from bottom, that's the 1st LinearLayout still visible, like the image below showed:
因此,当按下
Button
(id: my_btn) 时,如何使用 EditText
显示 2nd LinearLayout
在1st LinearLayout
之上,而不是在下方附加2nd LinearLayout
1st LinearLayout
以编程方式?
So, when
Button
(id: my_btn) is pressed how to show the 2nd LinearLayout
with EditText
on top of the 1st LinearLayout
instead of appending 2nd LinearLayout
below 1st LinearLayout
programmatically?
推荐答案
使用具有两个孩子的 FrameLayout.这两个孩子将重叠.这实际上是在 Android 的其中一个教程中推荐的,这不是黑客...
Use a FrameLayout with two children. The two children will be overlapped. This is recommended in one of the tutorials from Android actually, it's not a hack...
以下是在 ImageView 上显示 TextView 的示例:
Here is an example where a TextView is displayed on top of an ImageView:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:src="@drawable/golden_gate" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_gravity="center_horizontal|bottom"
android:padding="12dip"
android:background="#AA000000"
android:textColor="#ffffffff"
android:text="Golden Gate" />
</FrameLayout>
这篇关于在我的情况下,如何以编程方式在另一个布局之上显示一个布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!