包括布局并排除另一个布局

时间:2022-04-01 22:58:16

In my project i have 3 xml files.

在我的项目中,我有3个xml文件。

My main layout

我的主要布局

and 2 layouts which i want to include in my main layout

和2个布局,我想包括在我的主要布局中

*big_buttons.xml* >contains big size buttons *small_buttons.xml* >contains the same buttons as above (same id's aswell) but they are smaller

* big_buttons.xml *>包含大尺寸按钮* small_buttons.xml *>包含与上面相同的按钮(同样的ID也是如此)但它们更小

By default i want the *big_buttons.xml* included, but id like to be able to "exclude" the *big_buttons.xml* and include the *small_buttons.xml* programmaticly after an onClickListener

默认情况下,我希望包含* big_buttons.xml *,但id喜欢能够“排除”* big_buttons.xml *并在onClickListener之后以编程方式包含* small_buttons.xml *

Is it possible to do something like this?

有可能做这样的事吗?

2 个解决方案

#1


1  

By default you can use setContentView(R.layout.big_buttons);, and then in your onClickListener you could do setContentView(R.layout.small_buttons);

默认情况下,您可以使用setContentView(R.layout.big_buttons);,然后在您的onClickListener中,您可以执行setContentView(R.layout.small_buttons);

If it's specific buttons you want excluded rather than the entire XML, I think you need to combine the 2 XML files and by default give the "big buttons" the attribute android:visibility="visible" and the "small buttons" android:visibility="gone".

如果你想要排除而不是整个XML的特定按钮,我认为你需要结合2个XML文件,默认情况下给“大按钮”属性android:visibility =“visible”和“小按钮”android:visibility = “水涨船高”。

Then programmatically you can do

然后以编程方式即可

    Button bigButton = (Button) findViewById(R.id.big_button);
    Button smallButton = (Button) findViewById(R.id.small_button);

    bigButton.setVisibility("View.GONE");
    smallButton.setVisibility("View.VISIBLE");

You'll want to use GONE rather than INVISIBLE because GONE excludes layout features like height and width, where INVISIBLE just doesn't display the button, but keeps space for it.

您将要使用GONE而不是INVISIBLE,因为GONE排除了高度和宽度等布局功能,其中INVISIBLE只是不显示按钮,但为其保留空间。

#2


0  

Check out View.setVisibility. You can use this on a layout manager, so that you can make entire groups of controls visible or invisible from Java code.

查看View.setVisibility。您可以在布局管理器上使用它,以便可以从Java代码中使整组控件可见或不可见。

#1


1  

By default you can use setContentView(R.layout.big_buttons);, and then in your onClickListener you could do setContentView(R.layout.small_buttons);

默认情况下,您可以使用setContentView(R.layout.big_buttons);,然后在您的onClickListener中,您可以执行setContentView(R.layout.small_buttons);

If it's specific buttons you want excluded rather than the entire XML, I think you need to combine the 2 XML files and by default give the "big buttons" the attribute android:visibility="visible" and the "small buttons" android:visibility="gone".

如果你想要排除而不是整个XML的特定按钮,我认为你需要结合2个XML文件,默认情况下给“大按钮”属性android:visibility =“visible”和“小按钮”android:visibility = “水涨船高”。

Then programmatically you can do

然后以编程方式即可

    Button bigButton = (Button) findViewById(R.id.big_button);
    Button smallButton = (Button) findViewById(R.id.small_button);

    bigButton.setVisibility("View.GONE");
    smallButton.setVisibility("View.VISIBLE");

You'll want to use GONE rather than INVISIBLE because GONE excludes layout features like height and width, where INVISIBLE just doesn't display the button, but keeps space for it.

您将要使用GONE而不是INVISIBLE,因为GONE排除了高度和宽度等布局功能,其中INVISIBLE只是不显示按钮,但为其保留空间。

#2


0  

Check out View.setVisibility. You can use this on a layout manager, so that you can make entire groups of controls visible or invisible from Java code.

查看View.setVisibility。您可以在布局管理器上使用它,以便可以从Java代码中使整组控件可见或不可见。