0

Android 2.1 アプリを開発しています。

LinearLayout クラスを定義しました:

public class MyTopBar extends LinearLayout {
   ...
}

次に、レイアウト xml ファイル ( content.xml) があります。

<LinearLayout>
    ...
</LienarLayout>

この RootActivity のコンテンツとしてRootActivity.java設定したい があります。MyTopBar

次に、拡張する MyActivity がありますRootActivity

public class MyActivity extends RootActivity{
       //set xml layout as content here    
}

content.xml を MyActivity のコンテンツとして設定したいと思います。

全体として、上記の方法を使用して、常に画面の上にMyTopBar配置する必要があるレイアウトを実現したいと考えています。延長されるその他のアクティビティの内容は、以下のとおりです。これを達成する方法は??RootActivity MyTopBar

4

2 に答える 2

1

1 次のように、カスタムをクラスLinearLayoutの xml レイアウトに直接追加できます。MyActivity

<LinearLayout>
    <com.full.package.MyTopBar 
       attributes here like on any other xml views
    />
    ...
</LinearLayout>

または、includeタグを使用して、カスタム ビューにレイアウトを含めることができます。

<LinearLayout>
    <include layout="@layout/xml_file_containing_mytopbar"
    />
    ...
</LinearLayout>

2 使用:

setContentView(R.layout.other_content);
于 2012-03-05T09:50:41.227 に答える
0

TopBar のレイアウトを空けて、Topbar を使用してその中に追加しlayout.addView(topbarObject); ます。ただし、これらの 2 つの xml ファイルView.inflate(other_content.xml)は、必要に応じて親 xml レイアウトを使用して膨張させ、追加することができます。removeView()親レイアウトとaddView()新しいレイアウト ファイルを使用できます。

編集:両方の質問の解決策として、たとえば親レイアウトを使用できます。次のように:

//Omitting the obvious tags
//parent.xml
<RelativeLayout
    android:id="@+id/parentLayout">
    <RelativeLayout
        android:id="@+id/topLayout">
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/contentLayout">
    </RelativeLayout>
</RelativeLayout>

コードで親レイアウトをコンテンツ ビューとして設定し、TopBar レイアウトのオブジェクトを作成して、topLayout に追加します。

setContentView(R.layout.parent);
MyTopBar topBar=new MyTopBar(this);
RelativeLayout toplayout=(RelativeLayout)findViewByid(R.id.topLayout);
topLayout.addView(topBar); //or you can directly add it to the parentLayout, but it won't work for the first question. So better stick to it.

必要な xml レイアウトをインフレートします。contentLayout に追加します。

RelativeLayout layout=(RelativeLayout)View.inflate(R.layout.content,null);
contentLayout.addView(layout);//Assuming you've done the findViewById on this.

他のコンテンツ xml を表示する必要がある場合は、次のコードを呼び出すだけです。

contentLayout.removeAllView();
RelativeLayout layout2=(RelativeLayout)View.inflate(R.layout.other_content,null);
contentLayout.addView(layout2);
于 2012-03-05T09:55:28.333 に答える