English 中文(简体)
将自定义视图放入另一个视图中,同时使用XML布局
原标题:Putting a custom view into another View, using XML layouts for both

我开始制作一款游戏,它将有三个视图叠加在一起,基本上是一个徽标/广告行、一个分数行和一个游戏行。首先,我尝试创建一个名为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语句),但它似乎没有任何区别。。。

最佳回答

如果我没记错的话,你用错了构造函数,试试这个:

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

[Edit] Forgot to mention that since you re creating the view from XML, Android will call this constructor and it also enables you to use custom attributes if you so wish.

问题回答

I am not sure of what your trying to do, please add some details : name of xml files your intents, what you want to do exactly with both.

但这里有一些一般性的建议:

Override other constructors as well in your custom view classes. One argument parameters are meant to load class from code, with a second, attributeset argument, it will be used for xml constructs.

当您在xml(父级)中引用自定义组件(子级)时,请使用子类的完全限定类名。(将加载其子布局)。完全限定为包名+类名

致以问候,Stéphane

你试着打电话了吗?

this.addView(View.inflate(getContext(),R.layout.score_bar_layout,null));

太简单了,我觉得自己像个白痴。。。

问题是,基础布局是水平的。只需添加一个android:orientation=“vertical”就足以使其正常工作。叹气所以,发生的事情是,酒吧正在显示,但在屏幕下面(实际上,在它的右边),所以它永远看不见。将方向更改为它想要的方法就解决了这个问题,这样酒吧就可以被看到,而不是离开屏幕。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签