【发布时间】:2016-06-06 14:13:23
【问题描述】:
我有一个代码来调用意图来显示正在运行的服务屏幕。但我希望它显示在一个片段中。但我无法做到这一点。我从 java 类“bottompage.java”中调用这个意图,它是页面下半部分的片段。但是当按钮被点击时,这个意图仍然占据了全屏。
活动主要:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_display);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragmentContainer1, new TopPage());
fragmentTransaction.add(R.id.fragmentContainer2, new BottomPage());
fragmentTransaction.commit();
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
Bottompage.java
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View secondview = inflater.inflate(R.layout.bottom_fragment,container,false);
Button dummy = (Button) secondview.findViewById(R.id.dummy);
dummy.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
final String APP_DETAILS_PACKAGE_NAME ="com.android.settings";
// Here you need to define the package name
final String SCREEN_CLASS_NAME ="com.android.settings.RunningServices";
Intent servicesintent = new Intent();
servicesintent.setAction(Intent.ACTION_VIEW);
//intent.setAction(Intent.ACTION_VIEW);
servicesintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
servicesintent.setClassName(APP_DETAILS_PACKAGE_NAME, SCREEN_CLASS_NAME);
BottomPage.this.getActivity().startActivity(servicesintent);
}
});
return secondview;
}
Main.xml 2个片段的xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:layout_width="157dp"
android:layout_height="wrap_content"
android:text="Second page"
android:id="@+id/tv2"
android:layout_gravity="center_horizontal"
android:layout_weight="0.06" />
<FrameLayout
android:id ="@+id/fragmentContainer1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:layout_weight="0.06"></FrameLayout>
<FrameLayout
android:id ="@+id/fragmentContainer2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_below="@+id/fragmentContainer1"></FrameLayout>
底部片段的xml文件 BottomPage.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/bottom_fragment">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="call services"
android:id="@+id/dummy"
android:layout_gravity="center_horizontal" />
如何在 bottompage.java 中创建意图“servicesintent”以显示片段“fragmentcontainer2”中正在运行的服务。目前它正在全面屏。
【问题讨论】:
-
intent 是一个新活动,不能作为当前屏幕的一部分显示。
标签: android xml android-fragments android-studio android-intent