Android:在线性布局之上放置一些东西

发布于 2025-01-03 22:16:58 字数 565 浏览 5 评论 0原文

我想做的是当按下按钮时在“2”(第二个 LinearLayout)顶部显示一个“框架”(或新布局)。我该怎么做呢?预先创建它并在未按下按钮时以某种方式隐藏它?

我有这种类型的布局:

layout

XML:

<LinearLayout>

    <LinearLayout>
    </LinearLayout>

    <LinearLayout>
        //here would be another view, only shown when a button is clicked
        <ViewFlipper>
        </ViewFlipper>
    </LinearLayout>

    <RelativeLayout      
    </RelativeLayout>

</LinearLayout>

What I want to do is show a "frame" (or new layout) on top of "2" (second LinearLayout), when a button would be pressed. How should I do it? Precreate it and make it somehow hidden if button not pressed?

I have this type of layout:

layout

XML:

<LinearLayout>

    <LinearLayout>
    </LinearLayout>

    <LinearLayout>
        //here would be another view, only shown when a button is clicked
        <ViewFlipper>
        </ViewFlipper>
    </LinearLayout>

    <RelativeLayout      
    </RelativeLayout>

</LinearLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

初吻给了烟 2025-01-10 22:16:58

使用 FrameLayout 来显示覆盖另一个 viewview。您可以将视图保持为 INVISIBLE 或在 xml 中使用 GONE,然后在单击 Button 时使其可见。

Use FrameLayout to show view over-lapping another view. You can keep the view as INVISIBLE or using GONE in the xml and then just make it visible when the Button is Clicked.

尘世孤行 2025-01-10 22:16:58

是的...您应该在 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);
              }
          }        
);

Yes...you should prepare it in xml and give it an id.then you can easily manage its visibility on button click using mLinearLayout.setVisibility(View.GONE); and mLinearLayout.setVisibility(View.VISIBLE); like:

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);
              }
          }        
);
寻找我们的幸福 2025-01-10 22:16:58

这里有两个选择:

正如您所说,预先创建布局并将可见性设置为 Visibility_Gone 为布局最初不显示,将 Visibitlity 设置为 View.Visible 以显示布局。

另一种方法是动态创建视图,并添加到指定索引上的父级,例如添加到线性布局使用之上:

linearLayout.addView(view, 0);

Here you have two options:

As you said pre-create layouts and set visibility to Visibility_Gone to layouts initially, not to be shown, set Visibitlity to View.Visible to display the layouts.

Another approach is to create views dynamically, and adding to parent on specified index, like to add on top of linearlayout use:

linearLayout.addView(view, 0);
云柯 2025-01-10 22:16:58

如果您想在单击按钮时显示任何视图,请首先将该视图放入 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);

If you want to show any view on button click then first put that view inside xml and make its visibility gone, and on button click make it visible. I have put imageview inside your code which visibility is set as gone so it wont show in layout.

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

For making image view visible,

imag1.seVisibility(View.VISIBLE);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文