我正在尝试创建 Activity 布局,如下所示:
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
上尝试任何东西。
请任何人帮助我设计上面显示的 Activity ,
最佳答案
你想实现什么?这就是你实际上在做的事情:
setContentView(R.layout.game_board);
->game_board 是 inflatet,带有子级的 layoutTopicGrade 和 layoutGames
topicGrade = (LinearLayout)findViewById(R.id.layoutTopicGrade);
->这是layoutTopicGrade的一个实例
scroll = new ScrollView(this);
->你创建一个 ScrollView
scroll.addView(topicGrade);
->将 topicGrade 添加到 ScrollView
此操作失败,因为 topicGrade 已经有一个父级 (layoutFilterContent)
this.setContentView(scroll);
->您将 ScrollView 设置为内容。
(为什么之前要设置R.layout.game_board为content?)
如果您只想将 ScrollView
中的 topicGrade 作为 ContentView
,为什么不创建具有此结构的 xml?
如果建议像你这样做,解决方案是:
topicGrade = (LinearLayout)findViewById(R.id.layoutTopicGrade);
((LinearLayout)topicGrade.getParent()).removeView(topicGrade)
但我不明白为什么你应该这样做^^
--编辑: 现在我已经得到了,这是您需要的:
<?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>
关于java - 以编程方式向 Android 中的 LinearLayout 添加垂直和水平滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20520439/