android - 如何在Android中默认打开一个 fragment ?如何签署被点击的按钮?

标签 android button android-studio fragment main-activity

我正在制作足球应用程序,它在屏幕顶部有 4 个按钮。每个按钮都会打开不同的 fragment 。现在,当我默认打开应用程序时,它会显示 4 个按钮和它们下方的空白字段,其中将显示 fragment 的内容。我应该怎么做才能默认打开其中一个 fragment (作为 MainActivity 但它应该保留 fragment )?此外,我如何签署(注意,标记)选择的按钮,以便用户看到现在选择了哪个按钮? 谢谢,如果有不清楚的地方告诉我,我会进一步解释

最佳答案

您可以使用 Activity 的布局 XML 静态添加 fragment :

<fragment android:name="com.example.myapp.myfragment"
              android:id="@+id/my_fragment"
              android:layout_width="match_parent"
              android:layout_height="match_parent" />

或者您可以在 Activity 启动时动态添加它:

Fragment myFragment = new MyFragment();       
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.fragment_container, myFragment).commit();

关于android - 如何在Android中默认打开一个 fragment ?如何签署被点击的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32284051/

相关文章:

android - Fused Location Provider 更新速度不够快

android - 在服务和 Activity 之间共享数据的最有效方式

android - horizo​​ntalScrollView 内部的布局,即屏幕大小

android - 按下按钮后改变按钮的外观?

iphone - 按下按钮时切换到另一个 View

java - 在Android Studio上使用TestNG运行演示程序

android-studio - 如何在 Android Studio 中创建不同的文件夹来组织源代码?

android - getmeasuredheight() 和 getmeasuredwidth() 在 View.measure() 之后返回 0

javascript - Jquery 中的 Css 之后和之前

android - 没有 Gradle - 找不到与给定名称匹配的资源(在 'value' 处,值为 '@integer/google_play_services_version' )