在Android中使用BaseActivity的不同活动中的Common Header

时间:2012-07-20 12:43:19

标签: android android-layout extends

我想要一次编写代码并在不同的活动中使用。我为此创建了Base Activity class。此外,不同活动中所有布局的标题都是相同的。我已经在<include layout >标记的帮助下完成了这项工作。

现在问题是我的BaseActivity代码没有运行。我第一次尝试这个时候对此并不太了解。

1.)BaseActivity代码如下:

package com.waheguru.app;

import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import android.view.View;
import android.view.View.OnClickListener;

import android.widget.Button;
import android.widget.Toast;

public abstract class BaseActivityMenu extends Activity {
    //action id
    private static final int ID_UP     = 1;
    private static final int ID_DOWN   = 2;
    private static final int ID_SEARCH = 3;
    private static final int ID_INFO   = 4;
    private static final int ID_ERASE  = 5; 
    private static final int ID_OK     = 6;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.header);

        ActionItem nextItem     = new ActionItem(ID_DOWN, "Book", getResources().getDrawable(R.drawable.menu_down_arrow));
        ActionItem prevItem     = new ActionItem(ID_UP, "Bookmark", getResources().getDrawable(R.drawable.menu_up_arrow));
        ActionItem searchItem   = new ActionItem(ID_SEARCH, "Find", getResources().getDrawable(R.drawable.menu_search));
        ActionItem infoItem     = new ActionItem(ID_INFO, "Info", getResources().getDrawable(R.drawable.menu_info));
        ActionItem eraseItem    = new ActionItem(ID_ERASE, "Clear", getResources().getDrawable(R.drawable.menu_eraser));
        ActionItem okItem       = new ActionItem(ID_OK, "OK", getResources().getDrawable(R.drawable.menu_ok));

        //use setSticky(true) to disable QuickAction dialog being dismissed after an item is clicked
        prevItem.setSticky(true);
        nextItem.setSticky(true);

        //create QuickAction. Use QuickAction.VERTICAL or QuickAction.HORIZONTAL param to define layout 
        //orientation
        final QuickAction quickAction = new QuickAction(this, QuickAction.VERTICAL);

        //add action items into QuickAction
        quickAction.addActionItem(nextItem);
        quickAction.addActionItem(prevItem);
        quickAction.addActionItem(searchItem);
        quickAction.addActionItem(infoItem);
        quickAction.addActionItem(eraseItem);
        quickAction.addActionItem(okItem);

        //Set listener for action item clicked
        quickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {          
            public void onItemClick(QuickAction source, int pos, int actionId) {                
                ActionItem actionItem = quickAction.getActionItem(pos);

                //here we can filter which action item was clicked with pos or actionId parameter
                if (actionId == ID_SEARCH) {
                    Toast.makeText(getApplicationContext(), "Let's do some search action", Toast.LENGTH_SHORT).show();
                } else if (actionId == ID_INFO) {
                    Toast.makeText(getApplicationContext(), "I have no info this time", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), actionItem.getTitle() + " selected", Toast.LENGTH_SHORT).show();
                }
            }
        });

        //set listnener for on dismiss event, this listener will be called only if QuickAction dialog was dismissed
        //by clicking the area outside the dialog.
        quickAction.setOnDismissListener(new QuickAction.OnDismissListener() {          
            public void onDismiss() {
                Toast.makeText(getApplicationContext(), "Dismissed", Toast.LENGTH_SHORT).show();
            }
        });
        Button books=(Button)findViewById(R.id.book);
        books.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent intent=new Intent(ExampleActivity.this,List_of_books.class);
                startActivityForResult(intent, 0);
            }
        });
        //show on btn1
        Button btn1 = (Button) this.findViewById(R.id.menu);
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                quickAction.show(v);
            }
        });
    }
}

2。)活动扩展了基本活动

package com.waheguru.app;

import android.app.Activity;
import android.os.Bundle;

public class ABCActivity extends BaseActivityMenu  {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
    }
}

任何人都可以帮助我做错事。

4 个答案:

答案 0 :(得分:21)

为此,您必须创建一个header.xml,它将包含在您的活动的每个布局中,如下所示

header.xml

<RelativeLayout>
  <TextView android:id="@+id/txtHeading"
      .... />
</RelativeLayout>

activity_main.xml中

<RelativeLayout>
  <!-- include your header here -->
  <include layout="@layout/header"
     ... />

  <!-- Rest of your views -->

</RelativeLayout>

BaseActivity

abstract class BaseActivity extends Activity {
  protected TextView txtHeading;
  public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
  }


  protected void setHeading(int resId) {
     if(txtHeading == null)
     txtHeading = findViewById(R.id.txtHeading);
     if(txtHeading != null)
       txtHeading.setText(resId);
  }
}

MainActivity

class MainActivity extends BaseActivity {
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      setHeading(R.string.heading_main);
   }
}

您可以在BaseActivity或BaseListActivity中添加所需数量的视图并管理常用内容。

答案 1 :(得分:2)

如果要使用活动进行继承,并且基本活动调用setContentView,之后实际活动调用setContentView,则最后一次调用将设置活动的布局。因此,如果您正在寻找一种解决方案,其中所有活动具有相同的标题组件,则有两种方式。

  1. 对于每个活动布局xml,您都包含该组件

  2. - 为baseActivity创建功能,例如setContent(int layout_id) - 你总是跟你的活动打电话。 -Baseactivity使用标头膨胀根视图,并将layout_id视图扩展到该布局。 - 然后用该组件调用实际的setContentView。

答案 2 :(得分:2)

我认为您应该使用Fragment来实现它,这可能会对您有所帮助。

1 - 在main.xml中,添加:

<fragment
    android:id="@+id/header"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    class="com.package.name.HeaderPanel" />

//remaining is same 

2 - 扩展BaseActivity的{​​{1}}:

FragmentActivity

3 - 现在,您可以使用public class BaseActivityMenu extends FragmentActivity { private static final String TAG = Default.class.getName() + " - "; private int mResLayoutId; public void onCreate(Bundle savedInstanceState, int resLayout){ super.onCreate(savedInstanceState); setContentView(resLayout); mResLayoutId = resLayout; switch(mResLayoutId){ // here change with your xml file case R.layout.home: // set here common control like header textview break; default: break; } } } 扩展Activity。这样就可以BaseActivity扩展Activity

FragmentActivity

答案 3 :(得分:0)

在代码中,您的基本活动称为ExampleActivity,但在您的子类中,您正在扩展BaseActivityMenu。不知道它的来源。

也许改变:

public class ABCActivity extends BaseActivityMenu

对此:

public class ABCActivity extends ExampleActivity

此外,我建议您将基本活动(ExampleActivity)定义为抽象类。 例如:

public abstract class ExampleActivity extends Activity

这样做不会将您的基类定义为具体,并且可以在出现问题时更容易调试。