我想如下創建活動的佈局: 添加垂直和水平滾動編程到的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
請人幫我設計上面顯示的活動,
我加你需要我的回答 – 2red13