片段重叠中的Android视图
所以我读了好几篇教程和很多关于So的帖子,但我还是没能做到这一点。我尝试使用动态添加的片段,但在片段中加载片段时,TextView和ImageView会保持重叠,而不是更新其值,如下所示: 替换时,碎片相互重叠 我将一个主要活动(扩展AppCompatActivity)分为两个布局:片段重叠中的Android视图,android,android-fragments,android-activity,Android,Android Fragments,Android Activity,所以我读了好几篇教程和很多关于So的帖子,但我还是没能做到这一点。我尝试使用动态添加的片段,但在片段中加载片段时,TextView和ImageView会保持重叠,而不是更新其值,如下所示: 替换时,碎片相互重叠 我将一个主要活动(扩展AppCompatActivity)分为两个布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.andr
<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:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_white"
android:orientation="vertical"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/upperFrameLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/lowerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
我错过了什么
编辑:添加了FightFragment onCreateView()-方法:
…当我粘贴这段代码时,我才意识到那些膨胀的视图都是不同的对象。我用一个我修改过的对象替换了它们,然后让方法返回,它就可以工作了。我想我必须仔细阅读视图和充气方法。使用fragmentTransaction。add()而不是replace()在片段提交上方使用此选项
fragmentTransaction.addToBackStack(null);
我不明白充气的方法是怎样的。我修改了片段onCreateView方法,如OP中所示,现在它可以工作了。你能将你的
onCreate()
方法包括进来吗?将android:background=“@color/background\u white”
添加到片段的根视图中,然后再次检查。确保“颜色/背景\白色”的值不透明。@PrashantJha这解决了重叠问题,但现在只有带占位符字符串的文本视图可见,文本视图中没有带正确字符串的布局。现在是文本颜色问题吗?请检查一下TextView
的文本颜色。@PrashantJha这是一个与我的fragments onCreateView方法有关的问题,因为我不明白膨胀是如何工作的。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
((TextView) inflater.inflate(R.layout.fragment_fight, container, false).findViewById(R.id.score_textView)).setText(getString(R.string.score, 0, 0));
((TextView) inflater.inflate(R.layout.fragment_fight, container, false).findViewById(R.id.opponent_textView)).setText(MainActivity.opponentName.toUpperCase());
final AnimatedVectorDrawableCompat animated = AnimatedVectorDrawableCompat.create(container.getContext(), R.drawable.loading_animated_vector);
animated.registerAnimationCallback(new Animatable2Compat.AnimationCallback() {
@Override
public void onAnimationEnd(Drawable drawable) {
animated.start();
}
});
((ImageView) inflater.inflate(R.layout.fragment_fight, container, false).findViewById(R.id.opponent_hand_imageview)).setImageDrawable(animated);
animated.start();
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fight, container, false);
}
fragmentTransaction.addToBackStack(null);