1

我正在使用 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 爱好者可以将我推向正确的方向......

提前致谢,祝编码愉快!-E。

4

1 回答 1

0

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

于 2011-06-02T23:02:09.783 回答