mro*_*627 5 android android-layout android-fragments kotlin
我正在尝试使用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()
}
}
Run Code Online (Sandbox Code Playgroud)
这是我尝试创建的片段。它只是在增加按钮按下时的回调的同时放大视图:
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
}
}
Run Code Online (Sandbox Code Playgroud)
这是主要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>
Run Code Online (Sandbox Code Playgroud)
最后是片段膨胀的布局:
<?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>
Run Code Online (Sandbox Code Playgroud)
我觉得这是一个主Activity的相当简单的设置,它具有可以与其他片段互换的FrameLayout。有人知道我可能做错了吗?
同时,我想我将尝试提出一个更简单的案例来尝试复制行为。
编辑:这样一个简单的解决方案。我忘了添加android:orientation="vertical"我的LinearLayout。这是更新的代码:
<?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>
Run Code Online (Sandbox Code Playgroud)
问题是活动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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5914 次 |
| 最近记录: |