从View构造函数(Android)内部扩展视图

时间:2011-06-02 20:30:52

标签: android inflate

我正在使用LayoutInflater来扩展View的构造函数中View的内部,如下所示:

    public class TrackHeader extends RelativeLayout {

        public TrackHeader( Context context ) {
            super( context );
            final LayoutInflater inflater = getLayoutInflater( );
            final RelativeLayout view = (RelativeLayout) inflater.inflate(
                    R.layout.trackheader, this, true );
            < more here >
        }

(我也尝试了以下内容)

        public TrackHeader( Context context ) {
            super( context );
            final LayoutInflater inflater = getLayoutInflater( );
            final RelativeLayout view = (RelativeLayout) inflater.inflate(
                    R.layout.trackheader, this, false );
            addView( view, new ViewGroup.LayoutParams(  ViewGroup.LayoutParams.MATCH_PARENT,    ViewGroup.LayoutParams.MATCH_PARENT ) );
            < more here >
        }
    }

XML很简单:

    <?xml version="1.0" encoding="UTF-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <TextView android:id="@+id/trackNumber"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_gravity="center_horizontal"
            android:textSize="16dip"
            />
        <Button android:id="@+id/waveformBtn"
            android:layout_width="match_parent"
            android:layout_height="40dip"
            android:layout_below="@+id/trackNumber"
            android:layout_gravity="center_horizontal"
            android:textSize="12dip"/>
        <CheckBox android:id="@+id/enabledCheck"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/waveformBtn"
            android:layout_gravity="center_horizontal"
            android:textSize="12dip"/>
        <CheckBox android:id="@+id/loopingCheck"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/enabledBtn"
            android:layout_gravity="center_horizontal"
            android:textSize="12dip"/>
     </RelativeLayout>

我以这种方式膨胀它的原因是类(TrackHeader)是一个嵌套/内部类,这似乎阻止我直接在XML中引用该类。好吧,当充气时,布局不起作用。顶部节点(RelativeLayout)的大小正确以填充TrackHeader,但子视图根本没有调整大小并保持(0,0,0,0)。好奇,如果有任何Android爱好者可以在正确的方向推动我...

Thanx提前和快乐编码! -E

1 个答案:

答案 0 :(得分:0)

我不确定它是否解释为什么 none 的子视图的大小,但“loopingCheck”元素的“layout_below”属性应该是“@ + id / enabledCheck”而不是比“@ + id / enabledBtn”。除此之外,没有明显的问题跳出来。