为什么我的2个自定义视图布局彼此重叠,尽管我使用的是LinearLayout

时间:2013-04-08 12:33:43

标签: android

我尝试在运行时添加自定义视图。

MainActivity.java

package com.example.bug;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        android.view.ViewGroup root = (android.view.ViewGroup)getWindow().getDecorView().findViewById(android.R.id.content);
        View v0 = this.getLayoutInflater().inflate(R.layout.custom_view, null);
        View v1 = this.getLayoutInflater().inflate(R.layout.custom_view, null);
        ((android.widget.TextView)v0.findViewById(R.id.textView1)).setText("Text view 1");
        ((android.widget.TextView)v1.findViewById(R.id.textView1)).setText("Text view 2");
        // I expect v1 will below v0 as we are using vertical LinearLayout.
        // However, they are overlapping each others.
        root.addView(v0);
        root.addView(v1);
    }
}

custom_view.xml

<?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="48dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >
    </EditText>

</LinearLayout>

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

</LinearLayout>

然而,我不明白。我希望第二个视图位于第一个视图的底部。但是,我得到了重叠的结果。

(通过Java方式)

enter image description here

我做错了什么步骤吗?谢谢。

注意,如果我通过XML方式而不是Java方式添加2个自定义视图,则可以正常工作。但是我仍然不明白为什么我的Java方法不起作用?

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <include layout="@layout/custom_view"/>
    <include layout="@layout/custom_view"/>
</LinearLayout>

(通过XML方式)

enter image description here

2 个答案:

答案 0 :(得分:2)

这是因为您没有将View添加到LinearLayout中的activity_main.xml,而是添加到根ViewGroup,而不是LinearLayout }}。直接引用您的LinearLayout并将其添加到其中,如下所示:

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/main_activity_linear_layout" >

</LinearLayout>

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LinearLayout root = (LinearLayout) findViewById(R.id.main_activity_linear_layout);
    View v0 = this.getLayoutInflater().inflate(R.layout.custom_view, null);
    View v1 = this.getLayoutInflater().inflate(R.layout.custom_view, null);
    ((android.widget.TextView)v0.findViewById(R.id.textView1)).setText("Text view 1");
    ((android.widget.TextView)v1.findViewById(R.id.textView1)).setText("Text view 2");

    // I expect v1 will below v0 as we are using vertical LinearLayout.
    // However, they are overlapping each others.
    root.addView(v0);
    root.addView(v1);
}

答案 1 :(得分:0)

我建议您在添加视图时在代码中明确指定线性布局参数(宽度和高度):

View v0 = this.getLayoutInflater().inflate(R.layout.custom_view, null);
View v1 = this.getLayoutInflater().inflate(R.layout.custom_view, null);
...

LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

root.addView(v0, lp);
root.addView(v1, lp);

确保您从其他课程导入LinearLayout.LayoutParams而不是LayoutParams