Я пытаюсь создать макет деятельности следующим образом: Добавить Вертикальная и горизонтальная прокрутка программно LinearLayout в Android
game_board.xml
код будет следующим:
<LinearLayout
android:id="@+id/layoutFilterContent"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="4"
android:background="#837E7C" >
<LinearLayout
android:id="@+id/layoutTopicGrade"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:padding="0dp"
android:background="#BCC6CC" >
</LinearLayout>
<LinearLayout
android:id="@+id/layoutGames"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="3"
android:padding="0dp"
android:background="#E5E4E2" >
</LinearLayout>
</LinearLayout>
java
код в GameBoardActivity.java
выглядит следующим образом:
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.view.Menu;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ScrollView;
public class GameBoardActivity extends Activity {
LinearLayout topicGrade;
LinearLayout gameContent;
ScrollView scroll;
ImageButton[] imgBtn;
@SuppressWarnings("deprecation")
@SuppressLint({ "NewApi", "InlinedApi", "ResourceAsColor" })
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_board);
topicGrade = (LinearLayout)findViewById(R.id.layoutTopicGrade);
scroll = new ScrollView(this);
scroll.setBackgroundColor(android.R.color.transparent);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
scroll.addView(topicGrade);
imgBtn = new ImageButton[15];
for(int i=0;i<15;i++){
imgBtn[i] = new ImageButton(this);
imgBtn[i].setId(i);
imgBtn[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
topicGrade.addView(imgBtn[i]);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.game_board, menu);
return true;
}
}
я получаю error
как: The specified child already has a parent. You must call removeView() on the child's parent first.
Я первый попытался добавить кнопки LinearLayout1
без ScrollView
, и я могу добавить кнопки, как я хочу, но когда я реализую ScrollView
я получаю сообщение об ошибке, как указано выше. Так что я застрял и не пытался что-либо на LinearLayout2
Пожалуйста, кто поможет мне проектировать деятельность, показанную выше,
я добавил XML вам нужно мой ответ – 2red13