0

拡張するクラスがありますLinearLayout

コンストラクターで私が呼び出す

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout view = (LinearLayout)inflater.inflate(R.layout.titlebar, this, true);

このビューでビューにアクセスできますが、描画されません。その理由は、このビューの mParent がまだ null であるためだと思います。しかし、なぜ?Partent はこれでなければなりません (LinearLayout を拡張するクラス)

ここでxml:

<RelativeLayout android:id="@+id/header"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="35px" 
android:layout_width="fill_parent"
android:background="@drawable/titlebar_bg">

<TextView android:layout_width="wrap_content" 
    android:textColor="#000000"
    android:text="test" 
    android:id="@+id/title" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="10pt" 
    android:layout_height="wrap_content"
    android:textSize="10pt"
    android:layout_marginTop="3dip"></TextView>

<TextView android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:text="10:09 AM" 
    android:id="@+id/time" 
    android:textColor="#000000"
    android:layout_alignParentRight="true" 
    android:layout_marginRight="5px"
    android:layout_marginTop="3px"
    android:textSize="10pt"></TextView></RelativeLayout>

解決

<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:layout_width="wrap_content" 
    android:textColor="#000000"
    android:text="test" 
    android:id="@+id/title" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="10pt" 
    android:layout_height="wrap_content"
    android:textSize="10pt"
    android:layout_marginTop="3dip"></TextView>

<TextView android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:text="10:09 AM" 
    android:id="@+id/time" 
    android:textColor="#000000"
    android:layout_alignParentRight="true" 
    android:layout_marginRight="5px"
    android:layout_marginTop="3px"
    android:textSize="10pt"></TextView>

merge タグを使用し、xml タグを使用します。

<package.Titlebar 
            android:id="@+id/testtitlebar" 
            android:layout_height="35px" 
            android:layout_width="fill_parent"
            android:background="@drawable/titlebar_bg"></package.Titlebar>

これはクラスでどのように見えるかです:

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.titlebar, this);

onLayout関数はありません!!! 私の欠点の一つ

4

2 に答える 2

0

試してみてください

LayoutInflater inflater = LayoutInflater.from(this);

アクティビティクラス内にいる場合、または

LayoutInflater inflater = LayoutInflater.from(YourActivity.this);

アクティビティ クラス内のインライン クラスにいる場合。

次に、アクティビティ内の別のビューに新しいビューを追加する場合 (my_canvas など):

//[YourActivity.] is needed when you are in an inline class!
//RelativeLayout is not mandatory, you can have any other layout there
final RelativeLayout canvas = (RelativeLayout) [YourActivity.]this.findViewById(R.id.my_canvas);
final View titleBar = inflater.inflate(R.layout.titlebar, canvas, false);

これで、この titleBar を必要な場所に追加できます。

アップデート

これは、inflate メソッドの APIdoc です。

public View inflate (int resource, ViewGroup root, boolean attachToRoot) 

導入されたバージョン: API レベル 1
指定された xml リソースから新しいビュー階層を拡張します。エラーが発生した場合は InflateException をスローします。

パラメータ ロードする XML レイアウト リソースの
リソースID (
R.layout.main_page など)返された階層のルート (attachToRoot が false の場合)
attachToRoot インフレート された階層をルート パラメーターにアタッチする必要があるかどうか。false の場合、ルートは XML のルート ビューの LayoutParams の正しいサブクラスを作成するためにのみ使用されます。

戻り値 インフレートされた階層のルート ビュー。root が指定され、attachToRoot が true の場合、これは root です。それ以外の場合は、インフレートされた XML ファイルのルートです。

于 2011-04-15T08:57:59.650 に答える
0

試してみてください: LinearLayout view = (LinearLayout)inflater.inflate(R.layout.titlebar, null, false);

私の知る限り、これはビューを返します。このルートは、膨張した XML のルートです。

于 2011-04-15T08:26:28.070 に答える