【问题标题】:How to show one layout on top of the other programmatically in my case?在我的情况下,如何以编程方式在另一个布局之上显示一个布局?
【发布时间】:2011-10-05 03:41:51
【问题描述】:

我的主布局 ma​​in.xml 只包含两个 LinearLayout:

  • 第一个LinearLayout 拥有一个VideoView 和一个Button
  • 第二个LinearLayout 拥有一个EditText,这个LinearLayout 已将可见性 值设置为“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)时,2ndLinearLayoutEditText字段被显示出来,Java代码如下: p>

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 LinearLayoutEditText 显示为在下面 1st LinearLayout说得通。

但是,我需要的是:当Button(id: my_btn) 被按下时,2nd LinearLayoutEditText 显示在 1st LinearLayout 之上,看起来像 2nd LinearLayoutEditText 从屏幕底部升起,而 2nd LinearLayoutEditText 从底部只占据屏幕的一部分,即第一个 LinearLayout 仍然可见,如下图所示:

所以,当按下Button(id: my_btn) 时,如何显示第二个 LinearLayoutEditText 第一个 LinearLayout 而不是以编程方式在 1st LinearLayout 下方附加 2nd LinearLayout

【问题讨论】:

    标签: android android-layout android-emulator android-widget android-manifest


    【解决方案1】:

    使用带有两个孩子的 FrameLayout。两个孩子会重叠。这实际上是在 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>
    

    【讨论】:

    • 在这段代码中,是什么让TextView 位于顶部?是因为它在列表中排名第二吗?
    • 正如 FrameLayout 文档所说:子视图绘制在堆栈中,最近添加的子视图位于顶部。考虑到 xml 布局是从上到下解析的,那么 TextView 项目被绘制在顶部作为 xml 中的最后一个。
    • 我正在尝试这个,但 Scrollview 作为 FrameLayout 的第一个孩子,TextView 作为第二个孩子。但是这里它没有在背景中显示滚动视图。任何帮助。
    • 谢谢大哥!这个答案正是我要找的!伟大的! @Raju:将以下 FrameLayout 放在布局的最后一个结束标记之前(整个屏幕)... 现在引用它并照常执行 onClick 或 onTouch 方法。
    • FrameLayout 在这里是正确的选择,但同样值得注意的是,RelativeLayout 在添加子视图时会以相同的方式堆叠在一起。 (我是Android新手,读完这个答案后错误地认为只有FrameLayout才能完成堆叠。哇哦)
    【解决方案2】:

    FrameLayout 不是更好的方法:

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

    例子:

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

    【讨论】:

      【解决方案3】:

      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>
      

      在 Java 中:

      final MaterialDialog materialDialog = (MaterialDialog) dialogInterface;
      
      FrameLayout frameLayout = (FrameLayout) materialDialog
              .findViewById(R.id.icon_frame_container);
      
      frameLayout.setOnTouchListener(
              new OnSwipeTouchListener(ShowCardActivity.this) {
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-25
        • 2018-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多