添加新片段时显示的按钮
Posted
技术标签:
【中文标题】添加新片段时显示的按钮【英文标题】:Button showing when adding new fragment 【发布时间】:2017-08-07 04:15:54 【问题描述】:我正在将一个片段添加到另一个片段之上(片段 A -> 片段 B),但是当我这样做时,第一个片段中的按钮会显示出来。我不明白为什么在另一个片段之上添加一个片段会从第一个开始显示内容。
这是第一个活动。它包含第一个片段(片段 A):
public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
);
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in androidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
这是片段 A 的代码,它调用第二个片段(片段 B):
public class MainActivityFragment extends Fragment
public MainActivityFragment()
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
return inflater.inflate(R.layout.fragment_main, container, false);
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
Button startButton = (Button) view.findViewById(R.id.start_button);
startButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
GameSetupFragment newGame = new GameSetupFragment();
getFragmentManager().beginTransaction()
.addToBackStack("MainActivity")
.replace(R.id.fragment, newGame)
.commit();
);
这是片段 B 的类:
public class GameSetupFragment extends Fragment
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
return inflater.inflate(R.layout.game_board_fragment, container, false);
以下是每个 xml 文件:
Activity_Main.xml(主活动):
<android.support.design.widget.CoordinatorLayout 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_
android:layout_
tools:context="com.blaine.tictactoe.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_
android:layout_
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_
android:layout_
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_
android:layout_
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
这是片段 B 的 xml (fragment_main.xml)
<TextView
android:layout_
android:layout_
android:text="@string/welcome"
android:textAppearance="@android:style/TextAppearance.Large"
android:id="@+id/textView"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
android:layout_marginStart="69dp"
android:layout_marginEnd="69dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="74dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/start_button"
android:layout_
android:layout_
android:text="@string/start"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="117dp"
app:layout_constraintLeft_toLeftOf="parent" />
<android.support.constraint.Guideline
android:layout_
android:layout_
android:id="@+id/guideline"
android:orientation="vertical"
app:layout_constraintGuide_begin="367dp"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="367dp" />
<android.support.constraint.Guideline
android:layout_
android:layout_
android:id="@+id/guideline2"
app:layout_constraintGuide_begin="20dp"
android:orientation="vertical"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="20dp" />
<android.support.constraint.Guideline
android:layout_
android:layout_
android:id="@+id/guideline3"
app:layout_constraintGuide_begin="74dp"
android:orientation="horizontal"
tools:layout_editor_absoluteY="74dp"
tools:layout_editor_absoluteX="0dp" />
</android.support.constraint.ConstraintLayout>
Content_main.xml:
<fragment 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/fragment"
android:name="com.blaine.tictactoe.fragments.MainActivityFragment"
android:layout_
android:layout_
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:layout="@layout/fragment_main" />
片段B代码(game_board_fragment.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_
android:layout_
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/colorAccent">
<TextView
android:layout_
android:layout_
android:text="This is just a test"/>
</RelativeLayout>
这是应用程序运行时的样子:
【问题讨论】:
尝试从 Content_main 布局中删除 tools:layout="@layout/fragment_main"。您必须使用 frgament tansaction 从您的 mainactivity onCreate 方法添加此片段 【参考方案1】:在您的 Content_main.xml 中使用 FrameLayout 并尝试在其中打开您的片段。例如
<FrameLayout
android:id="@+id/main_fragment"
android:layout_
android:layout_ />
在 MainActivity 的 onCreate 中,使用以下代码打开您的第一个片段,如下所示:
String fragmentName = targetFragment.getClass().getName();
FragmentManager manager = getSupportFragmentManager();
Fragment targetFragment = new MainActivityFragment();
manager.popBackStack();
manager.beginTransaction()
.replace(R.id.main_fragment, targetFragment, fragmentName)
.addToBackStack(fragmentName)
.commit();
【讨论】:
我刚刚想通了。但这是正确的答案。感谢您的回答。以上是关于添加新片段时显示的按钮的主要内容,如果未能解决你的问题,请参考以下文章