0

我有一个线性布局,我正在以编程方式向它添加新的孩子。但是,当我预计它们会在 y 轴上堆叠时,它们都在 z 轴上堆叠在一起。我错过了什么?

布局是:

features.xml(具有以下子项的容器)

  • 特色标签.xml
  • 特色标签.xml
  • 特色标签.xml
  • 特色标签.xml
  • 特色标签.xml

精选.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>

特色标签.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>

代码

*将特色标签添加到特色*

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);
        }       
    }
4

1 回答 1

1

featured.xml将您的实际更改为 aLinearLayout并给它一个 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);
        }  
于 2012-09-26T22:14:02.153 回答