Android:合并XML和以编程方式生成布局

3

我有一个需求,需要将通过编程生成的布局与XML结合起来。我应该采取什么方法?请告诉我。谢谢。

生成布局:

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    LinearLayout audio = new LinearLayout(this);

    audio.setGravity(Gravity.BOTTOM | Gravity.CENTER);

    RecordButton = new RecordButton(this);
    audio.addView(RecordButton,
                new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.CENTER | Gravity.BOTTOM));

    PlayButton = new PlayButton(this);
    audio.addView(PlayButton,
                new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.CENTER | Gravity.BOTTOM));

    setContentView(audio);
}

xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:gravity="top">

<Button
android:id="@+id/next"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="130dp"
android:background="#54C571"
android:text="Next"
android:textColor="#FFFFFF"
android:textSize="23sp"
android:textStyle="bold" />

some_view_in_xml.addView(generated_layout); 这个怎么样? - K Neeraj Lal
@KNeerajLal 线性布局怎么办?我应该如何包含它们?是这样的 audio.xml.addView(generated) 吗?我尝试了一下,但它说无法解析 XML。 - Iman Yasmin
展示你的 XML 布局。 - K Neeraj Lal
@KNeerajLal 更新了 xml。该 xml 在页面中央有一个按钮,生成的布局包括页面底部的 2 个按钮。 - Iman Yasmin
1个回答

3
创建一个容器视图来容纳你的视图,并将生成的视图添加到其中。
这是xml代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:gravity="top">

    <Button
        android:id="@+id/next"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="130dp"
        android:background="#54C571"
        android:text="Next"
        android:textColor="#FFFFFF"
        android:textSize="23sp"
        android:textStyle="bold" />

    <!-- create a container view to hold your view -->
    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/next" />

</RelativeLayout>

在您的代码中,
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.the_above_layout);

    LinearLayout container = (LinearLayout) findViewById(R.id.container);


    LinearLayout audio = new LinearLayout(this);

    audio.setGravity(Gravity.BOTTOM | Gravity.CENTER);

    RecordButton = new RecordButton(this);
    audio.addView(RecordButton,
                new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.CENTER | Gravity.BOTTOM));

    PlayButton = new PlayButton(this);
    audio.addView(PlayButton,
                new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.CENTER | Gravity.BOTTOM));


    // add the view to your container
    container.addView(audio);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接