在片段视图之间切换
在 xml 布局文件中声明片段的标准方法是
<LinearLayout ...>
<fragment class="com.example.SomeFragment"
</LinearLayout>
SomeFragment 是一个 java 类,定义如下:
class SomeFragment extends Fragment {
...
}
让我们说,我有 3 个片段;片段 1、片段 2 和片段 3。当用户启动应用程序时,我向他们显示fragment1,当他们单击按钮时,我将fragment1 替换为fragment2,等等。
在单个布局xml 文件中定义3 个片段的最佳方法是什么?
The standard way to declare fragments in a xml layout file is
<LinearLayout ...>
<fragment class="com.example.SomeFragment"
</LinearLayout>
where SomeFragment is a java class defined like
class SomeFragment extends Fragment {
...
}
Lets say, I have 3 fragments; fragment1, fragment2, and fragment3. When the user launches the app, I show them fragment1, and when they click on a button, I replace the fragment1 with fragment2, etc.
What is the best approach to define the 3 fragments in a single layout xml file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为此,您应该使用 FrameLayout,这样您就不必在 XML 中指定片段类,并且它不限于一个类。
然后你可以像这样在代码中设置片段
You should use a FrameLayout for that, that way you don't have to specify the fragment class in the XML and that way it is not limited to one class.
and than you can set the fragment in the code like this
我给出了一个在片段中的两个布局之间切换的示例:
首先声明一个包含两个片段的布局:(这取决于您想要在布局中包含多少个片段)
fragment_layout_example.xml
上面的布局将显示两个片段 Fragment1 和 Fragment2。
对于 Fragment1,我已声明容器,因为容器的内容将在运行时更改。所以这里没有声明
Fragment
类。有关此检查的更多信息http://developer.android.com/training/ basics/fragments/fragment-ui.html
然后创建一个继承
Activity
的 FragmentExampleActivity 类。如果您在向后兼容模式下使用 Fragment,则扩展FragmentActivity
要为两个片段创建布局,请创建两个扩展
Fragment
的类,以同样的方式为第二个 Fragment 创建 Fragment 类并设置 现在的布局
如果您想通过单击按钮将 Fragment1 中的片段布局切换到另一个布局,请创建另一个类(例如 Fragment3.java)并设置要切换的布局,并在Fragment1.java
现在要再次返回第一个片段,您可以单击后退按钮。但如果您想通过单击按钮返回,请在 Fragment3.java 中编写以下代码,
谢谢!希望它能帮助你...
I am giving an example to switch between two layouts in a fragments:
First declare a layout with two fragments:(it depends on how many fragments you want in your layout)
fragment_layout_example.xml
Above layout will display two fragments Fragment1 and Fragment2.
For Fragment1 I had declared the container as content of the container is going to changed at runtime. So not declared the
Fragment
class here. for more on this checkhttp://developer.android.com/training/basics/fragments/fragment-ui.html
Then create a class FragmentExampleActivity which extends
Activity
. In case you are using Fragment in Backward compatibility mode then extendFragmentActivity
To create a layout for two fragments create two classes which extends
Fragment
Same way create Fragment class for second Fragment and set the layout
Now If you want to switch your fragment layout in Fragment1 to another layout on click of a button then create another class say Fragment3.java and set the layout you want to switch and write the below code inside the Fragment1.java
Now to come back again on the first Fragment you can click on back button. But if you want to come back on click of button then write the below code in Fragment3.java
Thanks! Hope it will help you...
在我的片段中,我创建了一个方法
然后在任何需要在执行进程后切换片段的例程中我做
In my fragment, I had created a method
And then in any of the routine that needs a switch of fragment after carrying a process i do