Android 替换片段会导致在framelayout中包装一个额外的视图层次结构
我目前正在开发一个应用程序,它有两个相互重叠的片段。下面的片段是一个导航轮,因此永远不会更改,但是当用户更改下面片段上的模式时,上面的片段会更改。我遇到的问题是,当我使用控制盘更改模式时,会将新视图添加到层次结构中,但不会删除旧视图,而且新添加的视图会包装在框架布局中,而我没有在任何地方定义 这是我在创建活动时使用的代码 RandomActivity.javaAndroid 替换片段会导致在framelayout中包装一个额外的视图层次结构,android,android-fragments,Android,Android Fragments,我目前正在开发一个应用程序,它有两个相互重叠的片段。下面的片段是一个导航轮,因此永远不会更改,但是当用户更改下面片段上的模式时,上面的片段会更改。我遇到的问题是,当我使用控制盘更改模式时,会将新视图添加到层次结构中,但不会删除旧视图,而且新添加的视图会包装在框架布局中,而我没有在任何地方定义 这是我在创建活动时使用的代码 RandomActivity.java @Override public void onCreate(Bundle savedInstanceState) {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.root);
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
modeFragment = new LottoModeFragment();
wheelFragment = new WheelFragment();
fragmentTransaction.add(R.id.mode_view, modeFragment);
fragmentTransaction.add(R.id.wheel_view, wheelFragment);
fragmentTransaction.commit();
}
private void switchFragments(ModeFragment fragment){
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.mode_view, fragment);
fragmentTransaction.commit();
}
public class ModeView extends RelativeLayout{
private RandomActivity activity;
private AppDisplaySizeInterface appDisplay;
public ModeView(Context context, AttributeSet attrs) {
super(context, attrs);
activity = (RandomActivity) getContext();
}
private void applyRoundedCorners(){
int width = (int) activity.getAppDisplaySizes().getModeWidth();
int height = (int) activity.getAppDisplaySizes().getModeHeight();
Bitmap original = Conversion.drawableToBitmap(this.getBackground(), width, height);
float radius = Float.parseFloat(getResources().getString(R.string.rounded_corner_radius));
RoundedCorners rounded = new RoundedCorners(original, radius, 0);
this.setBackgroundDrawable(rounded);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
appDisplay = activity.getAppDisplaySizes();
super.onMeasure(MeasureSpec.makeMeasureSpec((int) appDisplay.getModeWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec((int) appDisplay.getModeHeight(), MeasureSpec.EXACTLY));
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
applyRoundedCorners();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
这是我用来切换片段的代码
RandomActivity.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.root);
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
modeFragment = new LottoModeFragment();
wheelFragment = new WheelFragment();
fragmentTransaction.add(R.id.mode_view, modeFragment);
fragmentTransaction.add(R.id.wheel_view, wheelFragment);
fragmentTransaction.commit();
}
private void switchFragments(ModeFragment fragment){
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.mode_view, fragment);
fragmentTransaction.commit();
}
public class ModeView extends RelativeLayout{
private RandomActivity activity;
private AppDisplaySizeInterface appDisplay;
public ModeView(Context context, AttributeSet attrs) {
super(context, attrs);
activity = (RandomActivity) getContext();
}
private void applyRoundedCorners(){
int width = (int) activity.getAppDisplaySizes().getModeWidth();
int height = (int) activity.getAppDisplaySizes().getModeHeight();
Bitmap original = Conversion.drawableToBitmap(this.getBackground(), width, height);
float radius = Float.parseFloat(getResources().getString(R.string.rounded_corner_radius));
RoundedCorners rounded = new RoundedCorners(original, radius, 0);
this.setBackgroundDrawable(rounded);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
appDisplay = activity.getAppDisplaySizes();
super.onMeasure(MeasureSpec.makeMeasureSpec((int) appDisplay.getModeWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec((int) appDisplay.getModeHeight(), MeasureSpec.EXACTLY));
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
applyRoundedCorners();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
这是根视图,其中声明了两个片段的两个占位符
root.xml
<org.random.wheel.RootView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:contentDescription="@string/root_desc"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/root_background">
<FrameLayout
android:id="@+id/mode_view"
android:contentDescription="@string/mode_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<FrameLayout
android:id="@+id/wheel_view"
android:contentDescription="@string/wheel_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</org.random.wheel.RootView>
为了更好地说明正在发生的事情,请查看hierachyviewer中的这些屏幕截图
这显然令人讨厌,而不是它应该如何工作。如何消除作为添加视图父级的冗余FrameLayout?我如何让安卓系统用另一个片段替换一个片段,而不是仅仅添加它
类似的问题似乎只有在片段用xml声明时才会出现,但正如您所看到的,我的片段是用java代码编写的。比如说
还有。我正在使用支持包
提前谢谢
编辑
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
modeView = (LottoModeView) inflater.inflate(R.layout.lotto_mode, container, false);
return modeView;
}
根据请求包括ModeView.java:
ModeView.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.root);
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
modeFragment = new LottoModeFragment();
wheelFragment = new WheelFragment();
fragmentTransaction.add(R.id.mode_view, modeFragment);
fragmentTransaction.add(R.id.wheel_view, wheelFragment);
fragmentTransaction.commit();
}
private void switchFragments(ModeFragment fragment){
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.mode_view, fragment);
fragmentTransaction.commit();
}
public class ModeView extends RelativeLayout{
private RandomActivity activity;
private AppDisplaySizeInterface appDisplay;
public ModeView(Context context, AttributeSet attrs) {
super(context, attrs);
activity = (RandomActivity) getContext();
}
private void applyRoundedCorners(){
int width = (int) activity.getAppDisplaySizes().getModeWidth();
int height = (int) activity.getAppDisplaySizes().getModeHeight();
Bitmap original = Conversion.drawableToBitmap(this.getBackground(), width, height);
float radius = Float.parseFloat(getResources().getString(R.string.rounded_corner_radius));
RoundedCorners rounded = new RoundedCorners(original, radius, 0);
this.setBackgroundDrawable(rounded);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
appDisplay = activity.getAppDisplaySizes();
super.onMeasure(MeasureSpec.makeMeasureSpec((int) appDisplay.getModeWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec((int) appDisplay.getModeHeight(), MeasureSpec.EXACTLY));
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
applyRoundedCorners();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
}您确定额外的
FrameLayout
不是来自自定义视图lottomeview
?lottomeview是一个空类,但它扩展了ModeView(它再次扩展了RelativeLayout)。我不认为我在ModeView中做了任何应该给我一个额外的框架布局的事情,但我现在将编辑这篇文章以包含ModeView.java。