这是否可能我可以在活动视图上添加片段视图,而无需指定"片段"在活动的布局xml文件中查看组件?我应该找哪个功能?
答案 0 :(得分:14)
嗯,片段的UI必须某处。如果你想要整个"内容视图"要成为片段,请将片段添加到android.R.id.content
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getSupportFragmentManager().findFragmentById(android.R.id.content)==null) {
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, new ToDoRosterListFragment())
.commit();
}
}
否则,在活动视图层次结构中的某个位置,您需要一个容器(通常是FrameLayout
)来放置片段的UI。通常,我们通过将容器放在布局资源中来实现。
答案 1 :(得分:6)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, MyFragment.newInstance())
.commit();
//Some your code here
}
android.R.id.content是整个应用屏幕的容器。 它可以与Fragment一起使用:
android.R.id.content ID值表示活动的整个内容区域的ViewGroup。
上面的代码会将Fragment创建的View插入到android.R.id.content标识的ViewGroup中。
答案 2 :(得分:2)
您需要在活动中使用布局来包含片段(最好是FrameLayout)。
<FrameLayout 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/container"
android:name="com.gdevelopers.movies.movies.FragmentMoreMovies"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
然后在活动中输入此代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new YourFragment())
.commit();
}
}
答案 3 :(得分:2)
如果您不想使用@CommonsWare答案,那么您必须按代码提供容器。然后你需要的功能是......
setContentView(View)
是的。如果检查活动类代码,您将看到可以使用INT(布局用int标识)或使用Views调用setContentView。因此,您可以动态创建一个视图组实例,保持对它的引用(您需要对XML生成的视图执行相同操作),并在那里添加您的片段。这是可能的,因为XML文件只是视图工厂类Inflater用于查找哪些视图子类必须实例化的参数,使用XML中提供的一组参数。显然,你可以手工完成。只需选择您要使用的任何布局类,例如,FrameLayout和:
public class Activity extends AppCompatActivity{
private FrameLayout root;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
root = new FrameLayout(this);
root.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
setContentView(root);
//go on
}
}
答案 4 :(得分:2)
简单地说,在创建片段时,我们必须使用我们的应用程序中的视图替换或添加片段的视图。要替换或添加片段,我们通常会在活动或片段中添加Framelayout或任何其他布局视图(作为片段视图容器)。
现在,如果您想要替换或添加片段的视图而不在活动中添加额外的视图容器。您只需访问AppCompatActivity
或Activity
提供的视图即可。
现在,您可以创建一个片段,而无需在您可以创建的活动中添加视图容器,
YourFragment fragment = new YourFragment();
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(android.R.id.content, fragment); //here, android.R.id.content is a view on which your fragment's view is replaced
transaction.commit();
答案 5 :(得分:2)
使用android容器而不是像
这样的自定义容器fragmentTransaction.replace(android.R.id.content,yourFragment);
答案 6 :(得分:2)
1.使用DecorView
获取FramLayout
(DecorView
)
2.添加Fragment
的容器视图
3.将path
添加到容器视图
答案 7 :(得分:2)
LinearLayout llRoot = findViewById(R.id.parent);
FragmentManager fragMan = getSupportFragmentManager();
FragmentTransaction fragTransaction = fragMan.beginTransaction();
YourFragment yourFragment = (YourFragment)fragMan.findFragmentByTag("yourFragment");
if (yourFragment == null) {
yourFragment = new YourFragment();
}
fragTransaction.replace(llRoot.getId(), yourFragment, "yourFragment");
fragTransaction.commit();
llRoot是一个LinearLayout,它包含您活动的不同视图对象
答案 8 :(得分:0)
如果您不想在视图中将特定位置分配给片段容器,则可以始终使用RelativeLayour
。我想如果没有容器我们就不能在视图中放置一个片段。
答案 9 :(得分:0)
如果您在活动窗口中有任何View
或说根视图,请使用view.getId()
(与android:id
相关)获取其ID并将该ID传递给fragmentTransaction.add()
作为容器ID。这只是一个示例:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = new View(this);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(view.getId(), new Fragment());
}