将自定义视图放入另一个视图,使用两者的XML布局

Pea*_*oto 1 android android-layout android-xml

我开始研究游戏,它将3个视图叠加在一起,基本上是徽标/广告行,分数行和游戏行.首先,我正在尝试使用名为Level_Score_Bar的类,该类使用名为score_bar_layout的XML布局.现在,我的主要XML代码看起来像这样(注意,我一直根据下面的建议编辑它,如果我修复它,我将停止编辑它):

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="fill_parent" android:layout_width="fill_parent">
    <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal" android:layout_width="fill_parent" android:id="@+id/Title_bar">
        <ImageView android:layout_height="wrap_content" android:id="@+id/imageView1" android:layout_width="wrap_content" android:src="@drawable/icon"></ImageView>
        <LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/linearLayout4">
        </LinearLayout>
    </LinearLayout>
    <pearsonartphoto.AJEG.Level_Score_bar android:layout_height="fill_parent" android:layout_width="wrap_content" android:id="@+id/Score_Bar">
    </pearsonartphoto.AJEG.Level_Score_bar>
    <LinearLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="vertical" android:id="@+id/Game_Row">
        <View android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/view3"></View>
    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Level_Score_bar.java看起来像这样:

public class Level_Score_bar extends RelativeLayout {
public Level_Score_bar(Context context, AttributeSet set, int defStyle) {
    this(context,set);
}

public Level_Score_bar(Context context, AttributeSet set) {
    super(context, set);
    this.addView(View.inflate(context,R.layout.score_bar_layout,null));
    Log.d(TAG,"Added view");
}
}
Run Code Online (Sandbox Code Playgroud)

score_bar_layout.xml看起来像这样

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent" android:layout_height="wrap_content">
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:text="@string/level" android:id="@+id/Level_text" android:layout_toRightOf="@+id/Level_text"></TextView>
    <TextView android:id="@+id/Current_Level" android:layout_width="wrap_content" android:text="TextView" android:layout_height="wrap_content" android:layout_alignParentTop="true"></TextView>
    <TextView android:layout_width="wrap_content" android:text="TextView" android:layout_height="wrap_content" android:layout_centerVertical="false" android:layout_centerHorizontal="true" android:id="@+id/Time_Left"></TextView>
    <TextView android:id="@+id/Score_Label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:text="@string/score"></TextView>
    <TextView android:layout_width="wrap_content" android:text="TextView" android:layout_height="wrap_content" android:layout_toRightOf="@+id/Score_Label" android:id="@+id/Score_Value"></TextView>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

问题是,我根本没有看到这个观点.我敢肯定我错过了一些小事,但我不能为我的生活弄清楚.我已经确认正在调用addView命令(通过一个Log.d语句),但它似乎没有任何区别......

gre*_*gen 5

如果我没记错的话,你正在使用错误的构造函数,请尝试使用此构造函数:

public Level_Score_bar(Context context, AttributeSet set, int defStyle) {
    super(context, set, defStyle);
    // ... 
}

[编辑]忘记提及,因为您是从XML创建视图,Android将调用此构造函数,它还允许您使用自定义属性,如果您愿意.