我要做的就是爲每個子活動單獨個XML,但有你的導航抽屜的所有XML IDS是相同的。
接下來,當您啓動擴展活動的任何子類時,請將捆綁的contextView id - R.layout.MY_SUB_ACTIVITY傳遞到捆綁包中,並實際爲您的超類中的每個子活動設置內容視圖。
之後,您可以使用findViewByID來查找抽屜,在超類中設置它們,然後從它自己的類中處理子類的視圖。
你的意見很可能返回null,因爲你之前,你的子類的運行你的父類的onCreate - 因此的setContentView沒有被調用,但(這樣findViewByID將無法正常工作)
編輯:
而且,您不必將所有導航抽屜ID命名爲相同 - 我只是覺得使用<include>
標記更容易,可以最大限度地減少xml文件中的冗餘。
例如:
子類 -
public class InviteActivity extends MainActivity {
public static void openActivity(Activity from_activity) {
Intent intent = new Intent(from_activity, InviteActivity.class);
Bundle bundle = new Bundle();
bundle.putInt(MainActivity.KEY_LAYOUT_ID, R.layout.invite_activity);
bundle.putBoolean(MainActivity.KEY_HAS_LEFT_DRAWER, true);
bundle.putBoolean(MainActivity.KEY_HAS_RIGHT_DRAWER, false);
intent.putExtra(MainActivity.KEY_MAIN_BUNDLE, bundle);
from_activity.startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
.....
}
父類:
public abstract class MainActivity extends Activity {
/*
* Bundle keys specifying if the new activity inheriting from MainActivity
* has a left and/or right navigation drawer.
*/
public static final String KEY_MAIN_BUNDLE = "com.smashingboxes.bevii.MAIN_BUNDLE_KEY";
public static final String KEY_HAS_RIGHT_DRAWER = "com.smashingboxes.bevii.HAS_RIGHT_DRAWER";
public static final String KEY_HAS_LEFT_DRAWER = "com.smashingboxes.bevii.HAS_LEFT_DRAWER";
public static final String KEY_LAYOUT_ID = "com.smashingboxes.bevii.LAYOUT_ID_KEY";
public static final String KEY_SECTION_TITLE = "comm.smashingboxes.bevii.SECTION_TITLE";
/*
* Boolean indicators for each activity specifying their active navigation
* drawers.
*/
private boolean hasRightDrawer;
private boolean hasLeftDrawer;
/* The current activity's layout id for setContentView */
private String sectionTitle;
private int contentID;
/* UI Elements */
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
/* Reference to the ActionBar title to toggle text on drawer changes. */
protected CustomTextView mTitle;
private ListView mDrawerListLeft;
private LeftNavigationAdapter leftAdapter;
private ListView mDrawerListRight;
private LeftMessagesAdapter rightAdapter;
/**
* Handles the navigation drawer set up for each class inheriting from main
* activity. A bundle must be specified indicating the number of navigation
* drawers present in the activity's UI.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!getIntent().hasExtra(KEY_MAIN_BUNDLE))
throw new IllegalArgumentException(
"Cannot access a subclass of MainActivity without including a main bundle.");
Bundle mainBundle = getIntent().getBundleExtra(KEY_MAIN_BUNDLE);
hasRightDrawer = mainBundle.getBoolean(KEY_HAS_RIGHT_DRAWER);
hasLeftDrawer = mainBundle.getBoolean(KEY_HAS_LEFT_DRAWER);
contentID = mainBundle.getInt(KEY_LAYOUT_ID);
sectionTitle = mainBundle.getString(KEY_SECTION_TITLE);
setContentView(contentID);
if (hasRightDrawer && hasLeftDrawer) {
handleLeftNavigationDrawer();
handleRightNavigationDrawer();
} else if (hasLeftDrawer) {
handleLeftNavigationDrawer();
}
}
這看起來像一個不錯的辦法。讓我來修補一下,如果它有效,我會回來接受。 – TheGeekNess
好的。讓我知道是否足夠清楚 - 如果需要,我可以提供一個代碼示例,但不想打擾,除非需要...... – Submersed
我現在唯一的困惑是,因爲Superclass oncreate在子類之前被調用,在超類創建被調用之前,我沒有時間傳入任何包。所以我猜我需要在超類中創建公共方法,並在子類oncreate開始後手動調用它們來設置UI? – TheGeekNess