在我的情况下,如何以编程方式在另一个上面显示一个布局?

时间:2021-10-29 13:26:01

My main layout main.xml simply contains two LinearLayouts:

我的主要布局main.xml只包含两个LinearLayouts:

  • The 1st LinearLayout hosts a VideoView and a Button,
  • 第一个LinearLayout托管一个VideoView和一个Button,
  • The 2nd LinearLayout hosts an EditText, and this LinearLayout has set the visibility value to "GONE" (android:visibility="gone")
  • 第二个LinearLayout托管一个EditText,这个LinearLayout已将可见性值设置为“GONE”(android:visibility =“gone”)

like below:

如下:

<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>

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:

我成功实现了一个功能,当按下Button(带有id my_btn)时,会显示带有EditText字段的第二个LinearLayout,其中包含以下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);

    }
}); 

With the above Java code, the 2nd LinearLayout with EditText is shown like appending below the 1st LinearLayout which makes sense.

使用上面的Java代码,带有EditText的第二个LinearLayout显示为在第一个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的第二个LinearLayout显示在第一个LinearLayout的顶部,看起来像第二个LinearLayout,EditText从屏幕底部升起,第二个带有EditText的LinearLayout仅从底部占据屏幕的一部分,即第一个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?

因此,当按下Button(id:my_btn)时,如何在第一个LinearLayout顶部显示带有EditText的第二个LinearLayout,而不是以编程方式在第一个LinearLayout下面添加第二个LinearLayout?

3 个解决方案

#1


177  

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...

使用带有两个孩子的FrameLayout。这两个孩子将重叠。这实际上是在Android的一个教程中推荐的,它不是黑客...

Here is an example where a TextView is displayed on top of an ImageView:

下面是一个示例,其中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>

在我的情况下,如何以编程方式在另一个上面显示一个布局?

#2


4  

The answer, given by Alexandru is working quite nice. As he said, it is important that this "accessor"-view is added as the last element. Here is some code which did the trick for me:

Alexandru给出的答案非常好。正如他所说,重要的是将这个“访问者”视图添加为最后一个元素。这里有一些代码可以解决这个问题:

        ...

        ...

            </LinearLayout>

        </LinearLayout>

    </FrameLayout>

</LinearLayout>

<!-- place a FrameLayout (match_parent) as the last child -->
<FrameLayout
    android:id="@+id/icon_frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>

</TabHost>

in Java:

在Java中:

final MaterialDialog materialDialog = (MaterialDialog) dialogInterface;

FrameLayout frameLayout = (FrameLayout) materialDialog
        .findViewById(R.id.icon_frame_container);

frameLayout.setOnTouchListener(
        new OnSwipeTouchListener(ShowCardActivity.this) {

#3


0  

FrameLayout is not the better way to do this:

FrameLayout不是更好的方法:

Use RelativeLayout instead. You can position the elements anywhere you like. The element that comes after, has the higher z-index than the previous one (i.e. it comes over the previous one).

请改用RelativeLayout。您可以将元素放在任何您喜欢的位置。之后的元素具有比前一个更高的z-index(即它超过前一个)。

Example:

例:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary"
        app:srcCompat="@drawable/ic_information"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is a text."
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_margin="8dp"
        android:padding="5dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:background="#A000"
        android:textColor="@android:color/white"/>
</RelativeLayout>

在我的情况下,如何以编程方式在另一个上面显示一个布局?

#1


177  

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...

使用带有两个孩子的FrameLayout。这两个孩子将重叠。这实际上是在Android的一个教程中推荐的,它不是黑客...

Here is an example where a TextView is displayed on top of an ImageView:

下面是一个示例,其中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>

在我的情况下,如何以编程方式在另一个上面显示一个布局?

#2


4  

The answer, given by Alexandru is working quite nice. As he said, it is important that this "accessor"-view is added as the last element. Here is some code which did the trick for me:

Alexandru给出的答案非常好。正如他所说,重要的是将这个“访问者”视图添加为最后一个元素。这里有一些代码可以解决这个问题:

        ...

        ...

            </LinearLayout>

        </LinearLayout>

    </FrameLayout>

</LinearLayout>

<!-- place a FrameLayout (match_parent) as the last child -->
<FrameLayout
    android:id="@+id/icon_frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>

</TabHost>

in Java:

在Java中:

final MaterialDialog materialDialog = (MaterialDialog) dialogInterface;

FrameLayout frameLayout = (FrameLayout) materialDialog
        .findViewById(R.id.icon_frame_container);

frameLayout.setOnTouchListener(
        new OnSwipeTouchListener(ShowCardActivity.this) {

#3


0  

FrameLayout is not the better way to do this:

FrameLayout不是更好的方法:

Use RelativeLayout instead. You can position the elements anywhere you like. The element that comes after, has the higher z-index than the previous one (i.e. it comes over the previous one).

请改用RelativeLayout。您可以将元素放在任何您喜欢的位置。之后的元素具有比前一个更高的z-index(即它超过前一个)。

Example:

例:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary"
        app:srcCompat="@drawable/ic_information"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is a text."
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_margin="8dp"
        android:padding="5dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:background="#A000"
        android:textColor="@android:color/white"/>
</RelativeLayout>

在我的情况下,如何以编程方式在另一个上面显示一个布局?