1

我需要以编程方式创建一个全屏 android 活动,如下图所示:屏幕应该是什么样子

这两个按钮应保留在屏幕底部。虚拟内容将由不同的组件(文本视图、单选按钮、复选框...)组成,并将动态填充。

这是我到目前为止的代码:

        //Main Layout
    FrameLayout lLayout = new FrameLayout(this);
    lLayout.setBackgroundColor(Color.parseColor("#0099cc"));
    lLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
    //Navigation layout
    LinearLayout l = new LinearLayout(this, null, R.style.ButtonBar);
    LayoutParams bottomLayout = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
    l.setLayoutParams(bottomLayout);
    l.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
    l.setBackgroundColor(Color.parseColor("#66000000"));
    l.setOrientation(LinearLayout.HORIZONTAL);


    LayoutParams buttLayout = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    //previous section button
    previousButton = new Button(this);
    previousButton.setLayoutParams(buttLayout);
    previousButton.setText("Previous section");
    previousButton.setOnClickListener(this);
    l.addView(previousButton);
    //next section button
    Button nextButton = new Button(this);
    nextButton.setLayoutParams(buttLayout);
    nextButton.setText("Next section");
    nextButton.setOnClickListener(this);
    l.addView(nextButton);

    //add components
    TextView tView = new TextView(this);
    tView.setText("Dummy text");
    tView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
    lLayout.addView(tView);
    lLayout.addView(l);
    setContentView(lLayout);

这是代码产生的内容: 在此处输入图像描述

有几点不能按预期工作: 1. 按钮位于顶部而不是底部。2. 按钮没有很好地展开 3. 我作为测试添加的 TextView 显示在按钮后面。我将在屏幕上有许多不同的小部件,并希望它们大于一个屏幕。我希望有一个滚动选项,但在应该位于屏幕底部的两个按钮后面看不到所有这些小部件。

4

2 回答 2

2

以下 xml 正是您需要的:

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/dynamiclayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/navigationlayout"
        android:orientation="vertical" >
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/navigationlayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="Previous Section" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="Next Section" />
    </RelativeLayout>

</RelativeLayout>

现在以编程方式膨胀动态布局并将所有动态视图添加到其中。

于 2013-07-12T13:38:48.557 回答
2

您的根视图是 a FrameLayout,仅用于一个子视图。它也经常用于创建重叠视图,因为 aFrameLayout的所有子级都将绘制在屏幕上的同一位置。

将您的替换FrameLayoutRelativeLayout. 确保更新LayoutParams引用以使用RelativeLayout.LayoutParams. 您还需要将导航 LinearLayout 设置为与父视图的底部对齐,如下所示:

RelativeLayout.LayoutParams lps = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lps.addRule(RelativeLayout.LayoutParams.ALIGN_PARENT_BOTTOM, true);

虽然我真的建议使用 XML。它会让你的生活变得更简单。

于 2013-07-12T13:38:58.113 回答