我开始制作一个游戏,它将有 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>
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");
}
}
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>
问题是,我根本看不到视图。我确定我错过了一些小东西,但我终生无法弄清楚。我已经确认正在调用 addView 命令(通过 Log.d 语句),但它似乎没有任何区别......