[英]How to add a fragment inside a table row programmatically?
我可以使用以下xml來實現:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/startoverviewfragmentLayoutId" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<fragment
android:id="@+id/fragment1"
android:name="com.xxxx.xxx.MyMediaPlayer"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
</TableLayout>
但是我怎么用Java代碼呢? 我嘗試了這個:
MyMediaPlayer mpv = new MyMediaPlayer(); //Fragment
TableRow tt = (TableRow)myView.findViewById(R.id.tableRow1);
tt.addView(mpv);
但是由於片段不是視圖,因此無法使用。 那我該怎么辦?
為了向后兼容,請更換您的
<fragment
android:id="@+id/fragment1"
android:name="com.xxxx.xxx.MyMediaPlayer"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
同
<FrameLayout
android:id="@+id/fragment1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
在您的活動中使用此代碼
MyMediaPlayer myf = new MyMediaPlayer();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.fragment1, myf);
transaction.commit();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.