Android 更改'中的布局;包括';以编程方式使用Kotlin

Android 更改'中的布局;包括';以编程方式使用Kotlin,android,android-studio,kotlin,Android,Android Studio,Kotlin,我正在写一个简单的游戏,我希望主屏幕上有3种布局选择,2手,右手或左手 我有一个用于控件的include。然而,我正在努力使布局以编程方式改变。从昨晚开始一直在寻找,但找不到方法,这可能吗 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/androi

我正在写一个简单的游戏,我希望主屏幕上有3种布局选择,2手,右手或左手

我有一个用于控件的
include
。然而,我正在努力使
布局
以编程方式改变。从昨晚开始一直在寻找,但找不到方法,这可能吗

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".GamePlay">
 <include
        android:layout_width="match_parent"
        android:layout_height="88dp"
        layout="@layout/hand_two" <!-- this is what needs to change depending on settings -->
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:id="@+id/handLayout"/>
</androidx.constraintlayout.widget.ConstraintLayout>

app:layout\u constraintBottom\u toBottomOf=“parent”
app:layout\u constraintEnd\u toEndOf=“parent”
app:layout\u constraintStart\u toStartOf=“parent”
android:id=“@+id/handLayout”/

如果您有多个视图,希望按需以编程方式加载它们,ViewStub就是解决方案之一


请访问like链接

如果您有多个视图希望按需编程加载,ViewStub就是解决方案之一


请访问animusmind的like链接

: 使用ViewStub而不是include:

<ViewStub
    android:id="@+id/layout_stub"
    android:inflatedId="@+id/message_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.75" />

答案收集自《动物心智》杂志的《KCopock》

: 使用ViewStub而不是include:

<ViewStub
    android:id="@+id/layout_stub"
    android:inflatedId="@+id/message_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.75" />

从KCopock收集的答案是不可能的。我建议在你包含的布局中改变可见性。可能是重复的否,这不是直接可能的。我建议在你包含的布局中改变可见性。可能的重复不是很理想,因为我在布局中看不到存根,但已经走了这条路。不过,上面的代码是用Java编写的,我对此一无所知。我想我已经让它工作了,谢谢,使用存根并不理想,因为我在布局中看不到它,但已经走了这条路。不过,上面的代码是用Java编写的,我对此一无所知。不过我想我已经成功了,谢谢