2012-03-10 79 views
0

我正在尝试制作可重复使用的标题。这是我的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="wrap_content" 
    android:background="@drawable/bg" > 

    <ImageButton 
     android:id="@+id/imagebuttonforheader" 
     android:layout_height="50dp" 
     android:layout_width="50dp" 
     android:layout_alignParentLeft="true" 
     android:background="@drawable/back_button" 
     /> 

    <ImageButton 
     android:id="@+id/imagebuttonforheader" 
     android:layout_height="50dp" 
     android:layout_width="50dp" 
     android:layout_alignParentRight="true" 
     android:background="@drawable/forward_button" 
     /> 

</RelativeLayout> 

我做了一个类是:

public class Header extends RelativeLayout { 
    Context context; 
    Activity activity; 

    public Header(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     this.context = context; 
     LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     li.inflate(R.layout.header, null, false); 
    } 
} 

,然后添加此布局到我的main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <my.testy.view.Header 
     android:id="@+id/headerOnMain" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" > 
    </my.testy.view.Header> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/headerOnMain" 
     android:text="@string/hello" /> 
</RelativeLayout> 

但它没有出现在应用起来。

我在这里做错了什么?

回答

1

你要连接您的充气布局,以自定义类:

li.inflate(R.layout.header, this, true); 
+0

感谢。有效。我不能相信我忘记了这一点。它已经杀了我几个小时! – prometheuspk 2012-03-10 16:37:02

+0

@ Prometheus87另外我没有看到你的整个'R.layout.header',但看看这篇文章(如果你没有这样做) - > http://developer.android.com/resources/文章/布局花招merge.html – Luksprog 2012-03-10 16:40:50