我如何在FragmentActivity中获得ActionBarActivity?

时间:2014-11-17 14:21:53

标签: android android-activity android-fragments

如何从ActionBarActivity Class extends FragmentActivity获取public class StartActivity extends FragmentActivity { GetMetrics_Class metrics; ActionBarActivity activity; ImageView imgHeader; ImageView img_Setting_note; ImageView img_Calendar; ImageView img_Mail; ImageView img_Review_note; ImageView img_Help; ImageView img_Setting; LinearLayout LFooter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_start); activity = this; //get width & height mobile . metrics = new GetMetrics_Class(activity); initializeVars(); //Padding Header HedaerSize(); //Padding footer FooterSize_Pading(); //Height layout FooterSize_Layout_Height(); getSupportFragmentManager().addOnBackStackChangedListener(new OnBackStackChangedListener() { public void onBackStackChanged() { int backCount = getSupportFragmentManager().getBackStackEntryCount(); if (backCount == 0) { finish(); } } }); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction().add(R.id.main_container, new FirstPage()).addToBackStack(null).commit(); } } private void initializeVars() { imgHeader = (ImageView) findViewById(R.id.imgHeader); img_Setting_note = (ImageView) findViewById(R.id.img_Setting_note); img_Calendar = (ImageView) findViewById(R.id.img_Calendar); img_Mail = (ImageView) findViewById(R.id.img_Mail); img_Review_note = (ImageView) findViewById(R.id.img_Review_note); img_Help = (ImageView) findViewById(R.id.img_Help); img_Setting = (ImageView) findViewById(R.id.img_Setting); } private void HedaerSize(){ int PTopHeader = (54 * GetMetrics_Class.GMetrics_height()) / 1280; int PBottomHeader = (60 * GetMetrics_Class.GMetrics_height()) / 1280; int PRightHeader = (249 * GetMetrics_Class.GMetrics_width()) / 800; int PLeftHeader = (248 * GetMetrics_Class.GMetrics_width()) / 800; imgHeader.setPadding(PLeftHeader, PTopHeader,PRightHeader , PBottomHeader); } private void FooterSize_Pading(){ int PTopFooter_Setting_note = (32 * GetMetrics_Class.GMetrics_height()) / 1280; int PBottomFooter_Setting_note = (24 * GetMetrics_Class.GMetrics_height()) / 1280; int PRightFooter_Setting_note = (36 * GetMetrics_Class.GMetrics_width()) / 800; int PLeftFooter_Setting_note = (36 * GetMetrics_Class.GMetrics_width()) / 800; img_Setting_note.setPadding(PLeftFooter_Setting_note, PTopFooter_Setting_note, PRightFooter_Setting_note, PBottomFooter_Setting_note); int PTopFooter_Calendar = (38 * GetMetrics_Class.GMetrics_height()) / 1280; int PBottomFooter_Calendar = (31 * GetMetrics_Class.GMetrics_height()) / 1280; int PRightFooter_Calendar = (44 * GetMetrics_Class.GMetrics_width()) / 800; int PLeftFooter_Calendar = (39 * GetMetrics_Class.GMetrics_width()) / 800; img_Calendar.setPadding(PLeftFooter_Calendar, PTopFooter_Calendar, PRightFooter_Calendar, PBottomFooter_Calendar); int PTopFooter_Mail = (34 * GetMetrics_Class.GMetrics_height()) / 1280; int PBottomFooter_Mail = (24 * GetMetrics_Class.GMetrics_height()) / 1280; int PRightFooter_Mail = (30 * GetMetrics_Class.GMetrics_width()) / 800; int PLeftFooter_Mail = (30 * GetMetrics_Class.GMetrics_width()) / 800; img_Mail.setPadding(PLeftFooter_Mail, PTopFooter_Mail, PRightFooter_Mail, PBottomFooter_Mail); int PTopFooter_Review_note = (40 * GetMetrics_Class.GMetrics_height()) / 1280; int PBottomFooter_Review_note = (33 * GetMetrics_Class.GMetrics_height()) / 1280; int PRightFooter_Review_note = (25 * GetMetrics_Class.GMetrics_width()) / 800; int PLeftFooter_Review_note = (25 * GetMetrics_Class.GMetrics_width()) / 800; img_Review_note.setPadding(PLeftFooter_Review_note, PTopFooter_Review_note, PRightFooter_Review_note, PBottomFooter_Review_note); int PTopFooter_Help = (31 * GetMetrics_Class.GMetrics_height()) / 1280; int PBottomFooter_Help = (24 * GetMetrics_Class.GMetrics_height()) / 1280; int PRightFooter_Help = (28 * GetMetrics_Class.GMetrics_width()) / 800; int PLeftFooter_Help = (28 * GetMetrics_Class.GMetrics_width()) / 800; img_Help.setPadding(PLeftFooter_Help, PTopFooter_Help, PRightFooter_Help, PBottomFooter_Help); int PTopFooter_Setting = (31 * GetMetrics_Class.GMetrics_height()) / 1280; int PBottomFooter_Setting = (17 * GetMetrics_Class.GMetrics_height()) / 1280; int PRightFooter_Setting = (30 * GetMetrics_Class.GMetrics_width()) / 800; int PLeftFooter_Setting = (30 * GetMetrics_Class.GMetrics_width()) / 800; img_Setting.setPadding(PLeftFooter_Setting, PTopFooter_Setting, PRightFooter_Setting, PBottomFooter_Setting); } public void FooterSize_Layout_Height(){ LFooter = (LinearLayout) findViewById(R.id.LFooter); LFooter.getLayoutParams().height = (115 * GetMetrics_Class.GMetrics_height()) / 1280; } }

这是所有代码:

activity = this;

注意:this activity = this;未收到GetMetrics_Class(activity)

注意:我需要将{{1}}传递给{{1}}以获取宽度和高度。

2 个答案:

答案 0 :(得分:3)

ActionBarActivity已经继承(是)FragmentActivity。

所以你需要做的就是实现ActionBarActivity的一个实例。

public class StartActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
    }
}

答案 1 :(得分:1)

  

如何从FragmentActivity扩展的Class中获取ActionBarActivity。

你做不到。 ActionBarActivity inherits from FragmentActivity。您无法将FragmentActivity的实例神奇地转换为ActionBarActivity的实例。

欢迎您StartActivity延长ActionBarActivity