我想用XML设计一个布局,并希望动态地复制它,取决于列表的大小。它必须在一个片段中完成。不幸的是,我无法找到如何以片段形式完成它。
这就是XML的想法:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<FrameLayout
android:id="@+id/complete_map_layout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ICH bin die MAP!! Ich möchte ein Bild werden"
android:id="@+id/textView7"
android:layout_gravity="center_horizontal" />
<!-- Erstellung einzelner Räume -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="20dp"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
android:id="@+id/roomRow">
<!-- Bezeichnung Raum -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.10"
android:orientation="vertical"
android:id="@+id/roomNameOne"
android:background="#ff0000">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/roomName"
android:layout_gravity="center_vertical"
android:rotation="-90"
android:text="PLATZHALTER"/>
</LinearLayout>
<!-- Inhalt Raum -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.40"
android:orientation="horizontal"
android:id="@+id/WeaponNameOne">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/testWeapon"
android:layout_gravity="center_vertical"
android:text="WAFFE"
android:background="#00ff00"/>
</LinearLayout>
<!-- Inhalt Raum2 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.40"
android:orientation="horizontal"
android:id="@+id/weaponNameTwo"
android:background="#0000ff">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/testWeapon2"
android:layout_gravity="center_vertical"
android:text="WAFFE"/>
</LinearLayout>
<!-- Bezeichnung Raum2 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="0.10"
android:rotation="90"
android:id="@+id/roomNameTwo"
android:background="#ff0000">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/roomName2"
android:layout_gravity="center_vertical"
android:text="PLATZHALTER"/>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView8"
android:layout_gravity="right|bottom" />
</FrameLayout>
</ScrollView>
这是java代码:
public class MapOverview extends Fragment {
Bundle extras;
View fragLayoutV;
Game game;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
fragLayoutV = inflater.inflate(R.layout.fragment_map, null);
extras = getActivity().getIntent().getExtras();
game = (Game) extras.get("GAME");
return fragLayoutV;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FrameLayout frame = (FrameLayout)fragLayoutV.findViewById(R.id.complete_map_layout);
//LinearLayout roomInflator = (LinearLayout)frame.findViewById(R.id.roomRow);
Log.e("RAUM", game.getRooms().get(0).getName());
for(Room r:game.getRooms()){
frame.addView((LinearLayout)frame.findViewById(R.id.roomRow));
}
}
}
当然,我这样做会出错。有人可以帮忙吗?
目标是得到这样的图片:
http://i.stack.imgur.com/3DS73.png
无论颜色如何^^
答案 0 :(得分:0)
每次要添加布局时,都需要重新扩充布局。您无法多次添加相同的View
。
尝试这样的事情:
LayoutInflater inflater = LayoutInflater.from(this.getContext());
for (Room r : game.getRooms()) {
View view = inflater.inflate(R.layout.your_duplicate_layout, frame, false);
// Do whatever you want with view
frame.addView(view);
}
R.layout.your_duplicate_layout
与id = roomRow的当前布局相对应。