从视图构造函数内部膨胀视图(Android)
我使用LayoutInflater在视图的构造函数中膨胀视图的内部,如下所示:从视图构造函数内部膨胀视图(Android),android,inflate,Android,Inflate,我使用LayoutInflater在视图的构造函数中膨胀视图的内部,如下所示: public class TrackHeader extends RelativeLayout { public TrackHeader( Context context ) { super( context ); final LayoutInflater inflater = getLayoutInflater( );
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。好奇的是,是否有安卓迷能把我推向正确的方向
Thanx提前和快乐编码-E.我不确定这是否解释了为什么没有一个子视图被调整大小,但是loopingCheck元素的layout_below属性应该是@+id/enabledCheck,而不是@+id/enabledBtn。除此之外,没有明显的问题