将片段嵌入活动后,片段之前的布局中的小部件无法渲染。片段后的小部件可以正确渲染。如果我将小部件从片段之前移动到片段之后,它们也会渲染。发生了什么事,我该如何解决?
这是布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".PlayerViewActivity">
<!-- the following textview won't render -->
<TextView
android:id="@+id/character_name_zzz"
style="@style/TextAppearance.AppCompat.Large"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="@android:color/holo_red_dark"
android:text="before" />
<FrameLayout
android:id="@+id/spell_list_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</FrameLayout>
<TextView
android:id="@+id/character_class_zzz"
style="@style/TextAppearance.AppCompat.Large"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="@android:color/holo_blue_dark"
android:text="after" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomNavigationView"
style="@style/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
app:menu="@menu/navigation">
</android.support.design.widget.BottomNavigationView>
</LinearLayout>
这是活动:
public class PlayerViewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player_view);
// init the fragment
FragmentManager fragMan = getSupportFragmentManager();
FragmentTransaction txn = fragMan.beginTransaction();
SpellListFragment spellListFragment = new SpellListFragment();
txn.replace(R.id.spell_list_container, spellListFragment);
txn.commit();
}
}
这是带有上面列出的代码的应用程序的屏幕截图。在工具栏下方和片段上方应有一个红色TextView,其硬编码单词为“ before”。但是没有:
我在日志中没有看到任何异常。我认为Choreographer的抱怨是因为我在调试器中暂停了onCreate的执行。
2019-06-19 21:48:53.430 25455-25455/tome W/System.err: Took: 99
2019-06-19 21:48:53.484 25455-25455/tome D/ViewRootImpl@9e4df81[PlayerViewActivity]: setView = DecorView@152a826[PlayerViewActivity] TM=true MM=false
2019-06-19 21:48:53.496 25455-25455/tome D/ViewRootImpl@46e1c22[SpellScrollActivity]: MSG_WINDOW_FOCUS_CHANGED 0
2019-06-19 21:48:53.506 25455-25455/tome I/Choreographer: Skipped 1861 frames! The application may be doing too much work on its main thread.
2019-06-19 21:48:53.542 25455-25455/tome V/InputMethodManager: Not IME target window, ignoring
2019-06-19 21:48:53.554 25455-25455/tome D/ViewRootImpl@9e4df81[PlayerViewActivity]: dispatchAttachedToWindow
2019-06-19 21:48:53.556 25455-25460/tome I/zygote64: Do full code cache collection, code=503KB, data=343KB
2019-06-19 21:48:53.557 25455-25460/tome I/zygote64: After code cache collection, code=503KB, data=289KB
2019-06-19 21:48:53.593 25455-25455/tome W/StaticLayout: maxLineHeight should not be -1. maxLines:2 lineCount:2
2019-06-19 21:48:53.605 25455-25460/tome I/zygote64: Do partial code cache collection, code=503KB, data=299KB
2019-06-19 21:48:53.606 25455-25460/tome I/zygote64: After code cache collection, code=503KB, data=299KB
2019-06-19 21:48:53.606 25455-25460/tome I/zygote64: Increasing code cache capacity to 2MB
2019-06-19 21:48:53.654 25455-25455/tome V/Surface: sf_framedrop debug : 0x4f4c, game : false, logging : 0
2019-06-19 21:48:53.655 25455-25455/tome D/ViewRootImpl@9e4df81[PlayerViewActivity]: Relayout returned: old=[0,0][0,0] new=[0,0][1080,1920] result=0x7 surface={valid=true 496339603456} changed=true
2019-06-19 21:48:53.685 25455-25686/tome D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, egl_color_buffer_format *, EGLBoolean) returns 0x3000, [1080x1920]-format:1
2019-06-19 21:48:53.685 25455-25686/tome D/OpenGLRenderer: eglCreateWindowSurface = 0x738bcdc3b0
2019-06-19 21:48:53.888 25455-25686/tome D/OpenGLRenderer: eglDestroySurface = 0x73907d6b30
2019-06-19 21:48:53.919 25455-25455/tome D/ViewRootImpl@46e1c22[SpellScrollActivity]: Relayout returned: old=[0,0][1080,1920] new=[0,0][1080,1920] result=0x5 surface={valid=false 0} changed=true
2019-06-19 21:48:53.921 25455-25455/tome D/ViewRootImpl@9e4df81[PlayerViewActivity]: MSG_RESIZED_REPORT: frame=Rect(0, 0 - 1080, 1920) ci=Rect(0, 72 - 0, 0) vi=Rect(0, 72 - 0, 0) or=1
2019-06-19 21:48:53.925 25455-25455/tome D/ViewRootImpl@9e4df81[PlayerViewActivity]: MSG_WINDOW_FOCUS_CHANGED 1
2019-06-19 21:48:53.952 25455-25455/tome V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@90f856e nm : tome ic=null
2019-06-19 21:48:53.953 25455-25455/tome I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
2019-06-19 21:48:54.035 25455-25455/tome D/ViewRootImpl@46e1c22[SpellScrollActivity]: Relayout returned: old=[0,0][1080,1920] new=[0,0][1080,1920] result=0x1 surface={valid=false 0} changed=false
当我从活动中注释掉txn.commit()
时,“之前” TextView呈现:
有人可以帮我吗?
答案 0 :(得分:1)
尝试如下使用ConstraintLayout,
<?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"
android:orientation="vertical">
<!-- the following textview won't render -->
<TextView
android:id="@+id/character_name_zzz"
style="@style/TextAppearance.AppCompat.Large"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:background="@android:color/holo_red_dark"
app:layout_constraintTop_toTopOf="parent"
android:text="before" />
<FrameLayout
android:id="@+id/spell_list_container"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/color_primary"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/character_name_zzz"
app:layout_constraintBottom_toTopOf="@+id/character_class_zzz"/>
<TextView
android:id="@+id/character_class_zzz"
style="@style/TextAppearance.AppCompat.Large"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:background="@android:color/holo_blue_dark"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
android:text="after" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomNavigationView"
style="@style/bottom_navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="@menu/navigation" />
答案 1 :(得分:1)
该问题似乎是由于您赋予权重的方式引起的,请尝试如下进行更改:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".PlayerViewActivity">
<!-- the following textview won't render -->
<TextView
android:id="@+id/character_name_zzz"
style="@style/TextAppearance.AppCompat.Large"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_dark"
android:text="before" />
<FrameLayout
android:id="@+id/spell_list_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
<TextView
android:id="@+id/character_class_zzz"
style="@style/TextAppearance.AppCompat.Large"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_dark"
android:text="after" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomNavigationView"
style="@style/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/navigation">
</android.support.design.widget.BottomNavigationView>
</LinearLayout>
此外,将LinearLayouts与layout_weight
一起使用将产生两次测量其每个子项的开销。因此,请考虑改用ConstraintLayout,因为它比其他布局更有效。
https://developer.android.com/training/improving-layouts/optimizing-layout
答案 2 :(得分:0)
该片段包含一个带有android:fitsSystemWindows="true"
的CoordinatorLayout。当我删除它时,该片段正确呈现。