我有下面的代码,问题是所有膨胀的视图都具有相同的 id(在 中设置player_item.xml
) - 所以当我旋转屏幕时,它们都恢复为相同的值。最简单的解决方案是删除这些 id 并让 android 自动生成它们,但我不能这样做,因为我需要它们在相对布局中定位。任何想法我该如何解决这个问题?
player_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/player_name"
style="@style/stats_edit_column"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/goals"
android:gravity="center_vertical" />
<EditText
android:id="@+id/goals"
style="@style/stats_edit_column"
android:layout_toLeftOf="@+id/assists"
android:inputType="number"
android:textSize="@dimen/text_size_normal" />
<EditText
android:id="@+id/assists"
style="@style/stats_edit_column"
android:layout_alignParentRight="true"
android:inputType="number"
android:textSize="@dimen/text_size_normal" />
</RelativeLayout>
代码
for (PlayerStats playerStats : allPlayers) {
View playerStatsView = getLayoutInflater().inflate(R.layout.player_item, null);
TextView playerName = (TextView) playerStatsView.findViewById(R.id.player_name);
playerName.setText(playerStats.getPlayerName());
EditText goalsEditText = (EditText) playerStatsView.findViewById(R.id.goals);
goalsEditText.setText(String.valueOf(playerStats.getGoals()));
EditText assistsEditText = (EditText) playerStatsView.findViewById(R.id.assists);
assistsEditText.setText(String.valueOf(playerStats.getAssists()));
editLayout.addView(playerStatsView);
}