[英]Add Vertical and Horizontal Scrolling Programmatically to LinearLayout in Android
I'm trying to create a layout of a activity as follows: 我正在尝试创建活动的布局,如下所示:
the game_board.xml
code is is follows: 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>
The java
code in GameBoardActivity.java
is as follows: 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;
}
}
I'm getting error
as: The specified child already has a parent. You must call removeView() on the child's parent first.
我收到以下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.
I first tried adding buttons to LinearLayout1
without ScrollView
, and I'm able to add the buttons as I want, but when I implement ScrollView
I'm getting the error as mentioned above. 我首先尝试在不使用ScrollView
情况下将按钮添加到LinearLayout1
,并且能够根据需要添加按钮,但是在实现ScrollView
时遇到了如上所述的错误。 So I got stuck and didn't try anything on LinearLayout2
所以我被卡住了,没有在LinearLayout2
上尝试任何东西
Please anyone help me to design the activity shown above, 请任何人帮助我设计上面显示的活动,
what do you want do achiev? 您想要做什么? this is what you actually doing: 这是您实际执行的操作:
setContentView(R.layout.game_board);
->game_board is inflatet with both childs layoutTopicGrade and layoutGames -> game_board与孩子的layoutTopicGrade和layoutGames一起膨胀
topicGrade = (LinearLayout)findViewById(R.id.layoutTopicGrade);
->this is a instance of layoutTopicGrade ->这是layoutTopicGrade的一个实例
scroll = new ScrollView(this);
->you create a scroll view ->您创建滚动视图
scroll.addView(topicGrade);
->you add topicGrade to the scroll view ->您将topicGrade添加到滚动视图
This failed, because topicGrade already has a parent (layoutFilterContent) 失败了,因为topicGrade已经有一个父对象(layoutFilterContent)
this.setContentView(scroll);
->you set the croll view as content. ->将滚动视图设置为内容。
(why did you set R.layout.game_board as content before?) (为什么您之前将R.layout.game_board设置为内容?)
if you only want topicGrade in a ScrollView
as ContentView
, why didn't you create a xml with this structure? 如果您只想在ScrollView
中将topicGrade作为ContentView
,为什么不使用这种结构创建xml?
if it is recommended to do it like you do, the solution is: 如果建议像您一样这样做,则解决方案是:
topicGrade = (LinearLayout)findViewById(R.id.layoutTopicGrade);
((LinearLayout)topicGrade.getParent()).removeView(topicGrade)
but i can't see why you should do it in this way ^^ 但我不明白为什么你要用这种方式^^
--edit: now i've got it, here is the you need: --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>
Each element can have exactly one parent, that's an XML file structure. 每个元素可以只有一个父对象,即XML文件结构。
In your layout xml file you defined layoutTopicGrade
as the child of layoutFilterContent
. 在布局xml文件中,将layoutTopicGrade
定义为layoutTopicGrade
的子layoutFilterContent
。 Then, in the code, you set layoutTopicGrade
as the child of the newly created ScrollView
. 然后,在代码中,将layoutTopicGrade
设置为新创建的ScrollView
。
You should wrap your layoutTopicGrade
the ScrollView
. 您应该将您的layoutTopicGrade
包装为ScrollView
。 Also, since you're not doing any dynamic configuration to the ScrollView
, there's no reason adding it in the code, you should do it straight in the layout xml file: 另外,由于您没有对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>
EDIT 编辑
In order to have layoutTopicGrade
take 25% of parent's width and layoutGames
take 75%, do the following: 为了使layoutTopicGrade
占据父级宽度的25%, layoutGames
占据75%的宽度,请执行以下操作:
layout_weight
and layout_width
from layoutTopicGrade
to the ScrollView
. 将layout_weight
和layout_width
从layoutTopicGrade
移动到ScrollView
。 ScrollView
's layout_weight
to .25
. 将ScrollView
的layout_weight
设置为.25
。 layoutGames
's layout_weight
to .75
. 将layoutGames
的layout_weight
设置为.75
。 layoutTopicGrade
's children should have layout_width
set to fill_parent
. layoutTopicGrade
的子级应将layout_width
设置为fill_parent
。 See this answer . 看到这个答案 。
ADDITION 加成
BTW, looks like the line this.setContentView(scroll);
顺便说一句,就像这行this.setContentView(scroll);
should be deleted. 应该删除。 Activity.setContentView
is used to set a view for the whole activity = whole screen. Activity.setContentView
用于设置整个活动=整个屏幕的视图。 But your ScrolView
is apparently only for the topic layout. 但是您的ScrolView
显然仅适用于主题布局。 Rather the whole game board is the activity's content view, as you use it a few lines of code earlier: setContentView(R.layout.game_board);
整个游戏板是活动的内容视图,因为您之前使用了几行代码: setContentView(R.layout.game_board);
(so keep that one). (因此保留该内容)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.