以编程方式将垂直和水平滚动添加到Android中的LinearLayout

时间:2013-12-11 13:28:22

标签: java android android-layout

我正在尝试按如下方式创建活动布局: 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>

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

}

我得到errorThe specified child already has a parent. You must call removeView() on the child's parent first.

我首先尝试在没有LinearLayout1的情况下向ScrollView添加按钮,我可以根据需要添加按钮,但是当我实现ScrollView时,我收到了上面提到的错误。所以我陷入困境,并没有尝试LinearLayout2上的任何事情 请任何人帮我设计上面显示的活动,

2 个答案:

答案 0 :(得分:2)

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

setContentView(R.layout.game_board);

- &gt; game_board同时包含两个孩子的layoutTopicGrade和layoutGames

topicGrade = (LinearLayout)findViewById(R.id.layoutTopicGrade);

- &gt;这是layoutTopicGrade的一个实例

scroll = new ScrollView(this);

- &gt;您创建滚动视图

scroll.addView(topicGrade);

- &gt;您将topicGrade添加到滚动视图

这失败了,因为topicGrade已经有一个父(layoutFilterContent)

this.setContentView(scroll);

- &gt;您将croll视图设置为内容 (为什么你之前将R.layout.game_board设置为内容?)

如果您只想将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>

答案 1 :(得分:1)

每个元素可以只有一个父元素,即XML文件结构。

在布局xml文件中,您将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%,请执行以下操作:

  1. layout_weightlayout_widthlayoutTopicGrade移至ScrollView
  2. ScrollView的{​​{1}}设置为layout_weight
  3. .25的{​​{1}}设置为layoutGames
  4. layout_weight的孩子应该.75设置为layoutTopicGrade
  5. 请参阅this answer


    <强> ADDITION

    BTW,看起来应该删除行layout_widthfill_parent用于设置整个活动=整个屏幕的视图。但是您的this.setContentView(scroll);显然只适用于主题布局。相反,整个游戏板是活动的内容视图,因为您之前使用了几行代码:Activity.setContentView(所以请保留一行)。