将2个视图合并为一个,并将一个视图放在另一个之下

时间:2015-09-01 01:39:08

标签: java android layout

我添加了2种布局类型(在默认布局类型的顶部),其中一种在某些行的顶部添加了一些名称,另一种布局将按钮添加到屏幕(和黑线)。 但是我希望顶部的栏有名称保持不变,并在循环中添加按钮,我可以滚动浏览。到目前为止,我写的代码是:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ScrollView scrollPictures = new ScrollView(this);
    RelativeLayout mainApp = new RelativeLayout(this);
    LinearLayout appLayout = new LinearLayout(this);
    appLayout.setOrientation(LinearLayout.VERTICAL);
    appLayout.setClipBounds(null);

    for(int x = 1; x <= 15; x++){
        View view = LayoutInflater.from(this).inflate(R.layout.button_layout, appLayout, false);


        Button StudentSubmission = (Button) view.findViewById(R.id.button1);
        StudentSubmission.setText("Button 1");
        StudentSubmission.setBackgroundColor(Color.LTGRAY);

        Button SoundButton = (Button) view.findViewById(R.id.button2);
        SoundButton.setText("Button 2");
        SoundButton.setBackgroundColor(Color.LTGRAY);

        appLayout.addView(view);

    }

    scrollPictures.addView(appLayout);
    View topmenuview = LayoutInflater.from(this).inflate(R.layout.topofmenu, mainApp, false);

    RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams q = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    q.addRule(RelativeLayout.BELOW, scrollPictures.getId());

    mainApp.addView(scrollPictures, p);
    mainApp.addView(topmenuview, q);
    setContentView(mainApp);
}

但是,使用此代码时,带有列名称的栏位于按钮列表的顶部而不是上面,导致顶部按钮被切断。有没有办法让按钮显示在列的名称下?

1 个答案:

答案 0 :(得分:0)

q.addRule(RelativeLayout.BELOW, scrollPictures.getId());

mainApp.addView(scrollPictures, p);
mainApp.addView(topmenuview, q);

应该是

q.addRule(RelativeLayout.BELOW, topmenuview.getId());

mainApp.addView(topmenuview, p);
mainApp.addView(scrollPictures, q);

正确?