我有一個視圖,其中包含一個YouTubePlayerSupportFragment,其周圍有一個小框用於樣式。我想將這個視圖的多個實例(垂直)添加到另一個視圖的頂部。我有它添加單個視圖的工作,但是當我嘗試添加多個時,他們只是彼此堆疊,以便只有最後添加的那個可見。我能夠證明這種情況正在發生,通過編程將第一個視圖添加更長時間,並且我可以在最上面的視圖下看到它的額外高度。添加到linearLayout的片段正在堆疊,導致僅添加最後一個片段可見
這裏是我的代碼:
fragment_youtube_video:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
android:layout_marginTop="5dp"
android:background="#FFFFFFFF">
<fragment
android:id="@+id/youTubeVideo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment" />
<TextView
android:id="@+id/videoTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="8dp"
android:textColor="#000000"
android:text="Video" />
</LinearLayout>
fragment_videos:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background">
<ImageView
android:id="@+id/topBar"
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="45dp" />
<ImageView
android:id="@+id/logoImageView"
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:contentDescription="@string/CD_logo"
android:src="@drawable/logo" />
<RelativeLayout
android:id="@+id/pagerTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/logoImageView"
android:background="#80000000"
android:paddingBottom="10dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:paddingTop="10dp"
android:layout_marginLeft="12dp"
android:layout_marginTop="5dp"
android:layout_marginRight="12dp" >
[REMOVED FOR SPACE]
</RelativeLayout>
<ScrollView
android:id="@+id/scrollableVideoBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/pagerTitle"
android:layout_alignRight="@+id/pagerTitle"
android:layout_below="@+id/pagerTitle">
<LinearLayout
android:id="@+id/videosLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#a0000000">
</LinearLayout>
</ScrollView>
</RelativeLayout>
VideosFragment.java:
private void addVideoViews() {
int count = 0;
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
for (String vidURL : videoURLs) {
new YoutubeFragment();
YoutubeFragment player = YoutubeFragment.newInstance(count, vidURL);
fragmentTransaction.add(R.id.videosLayout, player, "x_" + count);
count++;
}
fragmentTransaction.commit();
}
我可以提供更多的代碼,但我相信這些是必要的部分。如果我錯誤地回答這個問題,請隨時告訴我。我很親密,我無法弄清楚爲什麼他們堆疊。
任何幫助,將不勝感激!
我不知道確切的金額。它可能會在1-10之間。我想我可以增加10個左右,然後這樣做。儘管如此,我希望保持活力。我可以動態添加這些佔位符視圖嗎? – PFranchise
我試圖使用鏈接示例中的代碼,但仍然會導致它們被堆疊。這讓我傾向於認爲我有一個佈局問題。 – PFranchise
是的,這將是最有可能的罪魁禍首。如果你把佔位符放在那可以正常工作?如果那是正確的,那麼至少你有一個正確的方法來讓它們一致。 –