线性布局没有分隔我添加的视图

时间:2012-09-26 21:30:21

标签: android android-layout

我有一个线性布局,我正在以编程方式添加新的子代。然而,当我预期它们将在y轴上堆叠时,它们都在z轴上彼此堆叠。我错过了什么?

布局是:

featured.xml(包含以下子项的容器)

  • featured_tab.xml
  • featured_tab.xml
  • featured_tab.xml
  • featured_tab.xml
  • featured_tab.xml

featured.xml

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

</LinearLayout>

featured_tab.xml

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

    <Button
        android:id="@+id/featuredContentBtn"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:background="#ffffff" />

    <ImageView
        android:id="@+id/featuredContentImg"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignBottom="@+id/featuredContentBtn"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:visibility="invisible"
        android:contentDescription="Featured Content Image" />

    <ImageView
        android:id="@+id/tabGradient"
        android:layout_width="fill_parent"
        android:layout_height="10dp"
        android:layout_alignBottom="@+id/featuredContentBtn"
        android:layout_alignParentLeft="true"
        android:src="@drawable/featured_tab_gradient"
        android:contentDescription="Tab Gradient" />

</RelativeLayout>

代码

*将featured_tabs添加到精选*

private void loadTabs() {
        Document doc = XMLParser.getDOM(Featured._xml);
        NodeList featuredNodes = doc.getElementsByTagName(Featured.TAG_FEATURE);

        this.removeAllViews();

        // loop through all featured nodes <Feature>
        for (int i = 0; i < featuredNodes.getLength(); i++) {
            FeaturedTab tab = (FeaturedTab)MainActivity.instance.getLayoutInflater().inflate(R.layout.featured_tab, null);
            Element element = (Element)featuredNodes.item(i);
            tab.setTitle(XMLParser.getValue(element, Featured.TAG_TITLE));
            tab.setImageURL(XMLParser.getValue(element, Featured.TAG_IMAGE_URL));
            tab.setLinkURL(XMLParser.getValue(element, Featured.TAG_LINK_URL));
            this.addView(tab);
        }       
    }

1 个答案:

答案 0 :(得分:1)

将您的featured.xml更改为实际为LinearLayout并为其提供ID:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/featured_linear_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

</LinearLayout>

在您的代码中,获取LinearLayout并以编程方式将其他布局添加到id

LinearLayout featured = (LinearLayout)findViewById(R.id.featured_linear_layout);

for (int i = 0; i < featuredNodes.getLength(); i++) {
            FeaturedTab tab = (FeaturedTab)MainActivity.instance.getLayoutInflater().inflate(R.layout.featured_tab, null);
            Element element = (Element)featuredNodes.item(i);
            tab.setTitle(XMLParser.getValue(element, Featured.TAG_TITLE));
            tab.setImageURL(XMLParser.getValue(element, Featured.TAG_IMAGE_URL));
            tab.setLinkURL(XMLParser.getValue(element, Featured.TAG_LINK_URL));
            featured.addView(tab);
        }