我的主布局 main.xml 只包含两个 LinearLayout:
- 第一个
LinearLayout
主持VideoView
和Button
, - 第二期
LinearLayout
主持EditText
, 而这个LinearLayout
已将 visibility 值设置为“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 代码:
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 代码,第二 LinearLayout
与 EditText
显示为 在下面添加 第一个 LinearLayout
这是有道理的。
但是,我需要的是:当Button
(id: my_btn) 被按下,第二 LinearLayout
与 EditText
显示在第一 LinearLayout
,看起来像 2nd LinearLayout
与 EditText
从屏幕底部升起,第二 LinearLayout
与 EditText
仅从底部占据屏幕的一部分,即第一个 LinearLayout 仍然可见,如下图所示:
所以,当 Button
(id: my_btn) 按下如何显示2nd LinearLayout
与 EditText
在第一 LinearLayout
而不是附加 2nd LinearLayout
低于第一 LinearLayout
以编程方式?
最佳答案
使用带有两个 child 的 FrameLayout。两个 child 会重叠。这实际上是在 Android 的一个教程中推荐的,它不是黑客......
这是一个 TextView 显示在 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>
关于android - 在我的情况下,如何以编程方式在另一个布局之上显示一个布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6690530/