1

我有一个动态添加所有显示元素的活动。根本没有用于活动的 xml。

该活动由以下控件组成:

  1. RelativeLayout(所有子视图所在的布局对象)
  2. TextView(页面的标题,位于RelativeLayout的顶部)
  3. ScrollView(包含所有数据控件的可滚动区域)
  4. LinearLayout(用于保存活动按钮的布局对象)

我想知道如何定义 ScrollView 位于 Title TextView 下方和 LinearLayout 按钮持有人上方,其中 LinearLayout 设置为活动页面底部

我曾尝试使用 RelativeLayout.LayoutParams 来设置规则,但似乎无法理解这样做的方式。任何帮助或教程链接将不胜感激

我已包含我的代码以查看是否有人可以提供帮助

    // declare the items for display
    RelativeLayout baseLayout = new RelativeLayout(this);   
    // add the customer name and number field.
    // NOTE: We will always require this widget regardless of dialog design fields
    tvCustomerNameNumber = new TextView(this);
    tvCustomerNameNumber.setTextSize(20);
    tvCustomerNameNumber.setText("Customer Name & Number");

    // build up the linear layout of controls
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);       

    // Scroll view.
    // NOTE: We will always need this widget to enable us to scroll the page
    // if too many items are added for the display size
    ScrollView sv = new ScrollView(this);
    sv.addView(ll);

    // buttons
    LinearLayout buttons = new LinearLayout(this);
    buttons.setOrientation(LinearLayout.HORIZONTAL);

    // button edit
    Button edit = new Button(this);
    edit.setId(EDIT_BUTTON_ID);

    // button save
    Button save = new Button(this);
    save.setId(SAVE_BUTTON_ID);

    // button cancel
    Button cancel = new Button(this);
    cancel.setId(CANCEL_BUTTON_ID);

    // add each button to the button layout
    buttons.addView(edit);
    buttons.addView(save);
    buttons.addView(cancel);

    // Scroll view Layout parameters
    RelativeLayout.LayoutParams scrollParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    scrollParams.addRule(RelativeLayout.BELOW, tvCustomerNameNumber.getId());
    scrollParams.addRule(RelativeLayout.ABOVE, buttons.getId());

    // buttons Layout parameters
    RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    buttonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    buttonParams.addRule(RelativeLayout.BELOW, sv.getId());

    // add the customer name number field to the base layout
    baseLayout.addView(tvCustomerNameNumber);
    // add the scroll view to the base layout
    baseLayout.addView(sv); //, scrollParams);      
    // add the buttons to the base layout
    baseLayout.addView(buttons, buttonParams);

    // set the content view
    this.setContentView(baseLayout);
4

2 回答 2

1

请参阅下面的链接,这可能有助于您实现这一目标:

以编程方式将项目添加到相对布局

如何以编程方式将多个 LinearLayouts 添加到一个视图中,然后添加到 ViewFlipper?

于 2012-02-28T12:54:30.317 回答
1

Deva 已经回答了您的问题,但在我看来,您可以像上面描述的那样定义一个 xml 布局,对其进行膨胀并以编程方式动态填充它......也许该布局最初将包含一个空的 LinearLayout,和/或没有文本为 TextView 设置,甚至可能将所有内容设置为 android:visibility="gone" 并在您添加/更新所有视图时显示它?

于 2012-02-28T13:32:25.800 回答