gpt4 book ai didi

java - 以编程方式向 Android 中的 LinearLayout 添加垂直和水平滚动

转载 作者:搜寻专家 更新时间:2023-11-01 08:54:02 25 4
gpt4 key购买 nike

我正在尝试创建一个 Activity 布局,如下所示: enter image description here

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;
}

}

我得到错误作为:指定的 child 已经有一个 parent 。您必须先对 child 的 parent 调用 removeView()。

我首先尝试在不使用 ScrollView 的情况下向 LinearLayout1 添加按钮,并且我可以根据需要添加按钮,但是当我实现 ScrollView 时,我'得到如上所述的错误。所以我卡住了,没有在 LinearLayout2

上尝试任何东西请任何人帮我设计上面显示的 Activity ,

最佳答案

你想做什么?这就是你实际做的:

setContentView(R.layout.game_board);

->game_board 是带有子 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com