![](/img/trans.png)
[英]Android:Vertical and horizontal scrolling with constrains on top and left
[英]Add Vertical and Horizontal Scrolling Programmatically to LinearLayout in 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>
GameBoardActivity.java
的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.
The specified child already has a parent. You must call removeView() on the child's parent first.
我首先嘗試在不使用ScrollView
情況下將按鈕添加到LinearLayout1
,並且能夠根據需要添加按鈕,但是在實現ScrollView
時遇到了如上所述的錯誤。 所以我被卡住了,沒有在LinearLayout2
上嘗試任何東西
請任何人幫助我設計上面顯示的活動,
您想要做什么? 這是您實際執行的操作:
setContentView(R.layout.game_board);
-> game_board與孩子的layoutTopicGrade和layoutGames一起膨脹
topicGrade = (LinearLayout)findViewById(R.id.layoutTopicGrade);
->這是layoutTopicGrade的一個實例
scroll = new ScrollView(this);
->您創建滾動視圖
scroll.addView(topicGrade);
->您將topicGrade添加到滾動視圖
失敗了,因為topicGrade已經有一個父對象(layoutFilterContent)
this.setContentView(scroll);
->將滾動視圖設置為內容。
(為什么您之前將R.layout.game_board設置為內容?)
如果您只想在ScrollView
中將topicGrade作為ContentView
,為什么不使用這種結構創建xml?
如果建議像您一樣這樣做,則解決方案是:
topicGrade = (LinearLayout)findViewById(R.id.layoutTopicGrade);
((LinearLayout)topicGrade.getParent()).removeView(topicGrade)
但我不明白為什么你要用這種方式^^
--edit:現在知道了,這是您需要的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="4" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:id="@+id/vertical_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="3">
<LinearLayout
android:id="@+id/horizontal_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
每個元素可以只有一個父對象,即XML文件結構。
在布局xml文件中,將layoutTopicGrade
定義為layoutTopicGrade
的子layoutFilterContent
。 然后,在代碼中,將layoutTopicGrade
設置為新創建的ScrollView
。
您應該將您的layoutTopicGrade
包裝為ScrollView
。 另外,由於您沒有對ScrollView
進行任何動態配置,因此沒有必要在代碼中添加它,因此應直接在布局xml文件中進行操作:
<ScrollView android:id="@+id/scrollViewTopicGrade"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/transparent" >
<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>
</ScrollView>
編輯
為了使layoutTopicGrade
占據父級寬度的25%, layoutGames
占據75%的寬度,請執行以下操作:
layout_weight
和layout_width
從layoutTopicGrade
移動到ScrollView
。 ScrollView
的layout_weight
設置為.25
。 layoutGames
的layout_weight
設置為.75
。 layoutTopicGrade
的子級應將layout_width
設置為fill_parent
。 看到這個答案 。
加成
順便說一句,就像這行this.setContentView(scroll);
應該刪除。 Activity.setContentView
用於設置整個活動=整個屏幕的視圖。 但是您的ScrolView
顯然僅適用於主題布局。 整個游戲板是活動的內容視圖,因為您之前使用了幾行代碼: setContentView(R.layout.game_board);
(因此保留該內容)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.