如何在Android上用Kotlin替换一个FrameLayout

我试图用Kotlin开发一个Android应用程序,但是当试图动态地移动碎片时,我遇到了一些麻烦。 我想要做的是用一个片段替换活动布局中的一个FrameLayout。 目前,每当我尝试运行我的应用程序,它只是在工具栏下显示一个白色的屏幕,导致我相信这个片段没有以我期望的方式被添加到FrameLayout。

这是我做第一笔交易的主要活动:

package net.ma.ttrobinson.kchan import android.content.Intent import android.os.Bundle import android.support.v7.app.AppCompatActivity import android.support.v7.widget.Toolbar import android.util.Log import android.view.Menu import android.view.MenuItem import android.view.View import android.widget.Button import android.widget.EditText import com.androidquery.callback.AjaxStatus import net.ma.ttrobinson.kchan.api.ChanThread import net.ma.ttrobinson.kchan.api.Request import org.jdeferred.DoneCallback import org.jdeferred.FailCallback import org.json.JSONArray import org.json.JSONObject class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val toolbar = findViewById(R.id.toolbar) as Toolbar setSupportActionBar(toolbar) getSupportFragmentManager().beginTransaction() .replace(R.id.main_content_frame, MainFragment()) .commit() } } 

这是我试图创建的片段。 它只是增加了一个视图,而添加一个按钮时按回调:

 package net.ma.ttrobinson.kchan import android.os.Bundle import android.support.v4.app.Fragment import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Button import android.widget.EditText import com.androidquery.callback.AjaxStatus import net.ma.ttrobinson.kchan.api.ChanThread import net.ma.ttrobinson.kchan.api.Request import org.jdeferred.DoneCallback import org.jdeferred.FailCallback /** * Holds the main content */ class MainFragment : Fragment() { companion object { val TAG = "MainFragment" } val debugBoard = "g" val debugThread = "48667796" override fun onCreateView(inflater : LayoutInflater, parent : ViewGroup?, savedInstanceState : Bundle?) : View { val v = inflater.inflate(R.layout.fragment_main, parent, false) val boardText = v.findViewById(R.id.board_text) as EditText val threadText = v.findViewById(R.id.thread_text) as EditText val request = Request(getActivity()) boardText.setText(debugBoard) threadText.setText(debugThread) val button = v.findViewById(R.id.submit) as Button button.setOnClickListener({ v -> val board = boardText.getText().toString() val thread = threadText.getText().toString().toInt() val promise = request.getThread(board, thread) promise.done({ thread -> val fm = getActivity().getSupportFragmentManager() fm.beginTransaction() .replace(R.id.main_content_frame, ThreadFragment(thread), ThreadFragment.TAG) .addToBackStack(null) .commit() }).fail({ result -> Log.v(TAG, "Failed to get thread") }) }) return v } } 

以下是主Activity使用setContentView的布局:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <include android:id="@+id/toolbar" layout="@layout/toolbar" /> <FrameLayout android:id="@+id/main_content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> 

最后这里是片段膨胀的布局:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/board_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/board_hint"/> <EditText android:id="@+id/thread_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="phone" android:hint="@string/thread_hint"/> <Button android:id="@+id/submit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/submit"/> </LinearLayout> 

我觉得这是一个相当简单的主Activity的设置,它有一个FrameLayout可以与其他片段交换出来。 有谁知道我可能做错了什么?

在此期间,我想我会试图做一个更简单的情况来尝试复制行为。

编辑:这样一个简单的解决方案。 我忘了在我的LinearLayout中添加android:orientation="vertical" 。 这是更新的代码:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <include android:id="@+id/toolbar" layout="@layout/toolbar" /> <FrameLayout android:id="@+id/main_content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> 

问题是Activity的LinearLayout应该有orientation="vertical"

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include android:id="@+id/toolbar" layout="@layout/toolbar" /> <FrameLayout android:id="@+id/main_content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>