0

を:一般的なコードレイアウトなどが - 私は、レイアウトを含めについて混乱しています(例アクションバー)

  1. のは、私はすべての私にこれを含め
  2. actionbar.xmlという名前のアクションバーのレイアウトを作ったとしましょう他のレイアウト

ここで、アクションバーのJavaコードをどのように書くべきですか?例えば、main.javaonclickの関数を書いたら?

second.javaに保存されている2番目のアクティビティは、どのように同じように使用できますか?アクションバーのonclickが定義されているクラスのオブジェクトを作成する以外の方法はありますか?

+0

あなたの問題についてはっきりさせてください。 – Goofy

+1

アクションバーのXMLコードを投稿してください – Blundell

答えて

0

すべてのアクティビティで何かを見えるようにするには、ヘッダーXMLとヘッダーのアクティビティを作成するという方法で行うことができます。

ヘッダーアクティビティでヘッダーXMLに関するコードを記述すると、アプリケーションの他のすべてのアクティビティはヘッダーアクティビティから拡張されます。

たとえば、how do I create a header or footer button bar for my android applicationがあります。

0

これを達成する方法があります。アクションバーの中間相コールバック関数を実装する必要があります。

2

あなたはこのようなaction_bar.xmlレイアウトしていた場合:

package com.your.package.ui.widget; 

public class ActionBar extends LinearLayout implements OnClickListener { 

    public ActionBar(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public ActionBar(Context context) { 
     super(context); 
    } 

     @Override 
     protected void onFinishInflate() { 
      super.onFinishInflate(); 

      findViewById(R.id.actionBarOpenButton).setOnClickListener(this); 
     } 

    @Override 
     public void onClick(View v) {     
      switch (v.getId()) { 
      case R.id.actionBarOpenButton: 
        // Do something 
        break; 
       default: 
       break; 
      } 
     } 
} 

あなたが希望:あなたはこのように見えたActionBar.javaというパッケージcom.your.package.ui.widget

でクラスを持っているでしょう

<?xml version="1.0" encoding="utf-8"?> 
<com.your.package.ui.widget.ActionBar xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/actionBar" 
    android:layout_width="fill_parent" 
    android:layout_height="58dip" 
    android:background="@drawable/action_bar_background" > 

<ImageButton 
     android:id="@+id/actionBarOpenButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@color/transparent" 
     android:contentDescription="open button" 
     android:src="@drawable/action_bar_open_button" /> 

</com.your.package.ui.widget.ActionBar> 

を次のように `activity_main.xml 'という別のレイアウトに含める:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <include layout="@layout/action_bar" /> 

    <!-- Rest of your layout --> 

</LinearLayout> 

これを任意のActvityに含めることができ、カスタムウィジェットはどこでも同じonClickイベントを実行します。

関連する問題