今天决定看看开源中国安卓版app,并试着重构一下。好的进入主题。
创建MainActivity
public class MainActivity extends ActionBarActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks, OnTabChangeListener, BaseViewInterface, View.OnClickListener, OnTouchListener {
从结构上可以开到MainAcitvity 是继承了ActionBarActivity的 而且ACtionBarActivity已经标记过时了,我不会处理过时的问题。接着它又实现了NavigationDrawerFragment.NavigationDrawerCallbacks(侧滑菜单界面的回调接口),TabHost.OntabChangeListener,BaseFragmentInterface(基类实现接口),View.OnclickListener,OntouchListener...
protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); if (AppContext.getNightModeSwitch()){ setTheme(R.style.AppBaseTheme_Night); }
AppBaseTheme_Night 是存放在value资源下的style资源
AppContext是 全局应用程序类: 用于保存和调用全局应用配置和访问网络数据。
在这里 我要写一个全局 的夜间模式的选择开关(方法)
public classAppcontext extends BaseApplication{
// 夜间模式 public static boolean getNightModeSwitch(){ return getPreferences().getBoolean(KEY_NIGHT_MODE_SWITCH,false); }
在这里 返回引用 要注意
SharePreference 用来保存程序的配置数据,可以有下面三种方法返回:
1.getSharePerences(int mode);
getPreferences(MODE_WORLD_WRITEABLE).edit().putBoolean("test", true).commit();
该方法是获取一个Activity默认配置,文件名为该Activity的类名
2. PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("test", true).commit();
这个用来获取整个APP的默认配置,文件名为 包名.xml
3.getSharedPreferences("name", MODE_WORLD_WRITEABLE).edit().putBoolean("test", true).commit();
自定义的配置文件名,
public class BaseApplication extends Application{ static Context _context; private static String PREF_NAME ="creativelocker.pref" @TargetApi(Build.VERSION_CODES.HONEYCOMB) public static SharedPreferences getPreferences(){ SharedPreferences pre = context().getSharedPreferences(PREF_NAME, Context.MODE_MULTI_PROCESS); return pre; } }
style theme color都是values资源文件
MainActivity 需要改动 -->调用 AppContext全局配置(相当于接口 ,写方法)-->BaseApplication(具体实现) 然后返回值
代码持续在 https://git.oschina.net/wujianxing/DougienOSC.git 更新。
...... 未完待续.......
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (AppContext.getNightModeSwitch()) { setTheme(R.style.AppBaseTheme_Night); } else { setTheme(R.style.AppBaseTheme_Light); } setContentView(R.layout.activity_main); ButterKnife.inject(this); initView(); AppManager.getAppManager().addActivity(this); handleIntent(getIntent()); // 注册听云的检测分析 NBSAppAgent.setLicenseKey("0ed0cc66c5cb45c0a91c6fa932ca99ac") .withCrashReportEnabled(true).withLocationServiceEnabled(true) .start(this); }
上图是开源中国主界面结构,包括正主界面和一个侧滑界面。仔细研究一下 它的布局文件。
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="net.oschina.app.ui.MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/realtabcontent" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" /> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/windows_bg" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="4dip" > <net.oschina.app.widget.MyFragmentTabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="4dip" /> <View android:layout_width="match_parent" android:layout_height="1px" android:background="?attr/lineColor" /> </RelativeLayout> <!-- 快速操作按钮 --> <ImageView android:id="@+id/quick_option_iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:contentDescription="@null" android:src="@drawable/btn_quickoption_selector" /> </FrameLayout> </LinearLayout> <!-- 左侧侧滑菜单 --> <fragment android:id="@+id/navigation_drawer" android:name="net.oschina.app.ui.NavigationDrawerFragment" android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent" android:layout_gravity="start" tools:layout="@layout/fragment_navigation_drawer" /> </android.support.v4.widget.DrawerLayout>
做一个抽屉菜单有2中方法
方式1.用SlidingDrawer:
http://developer.android.com/reference/android/widget/SlidingDrawer.html
方式2.用DrawerLayout:
http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html
上面用的是
<android.support.v4.widget.DrawerLayout
好的 ,我来做一个简单的抽屉界面的例子。
创建一个带XML的Activity,在XML 中我写成这样
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- 主要内容的视图--> <!-- main content must be the first element of DrawerLayout because it will be drawn first and drawer must be on top of it --> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="主界面" android:id="@+id/textView" android:layout_gravity="center_horizontal|top" android:layout_margin="30dp" android:textSize="30dp" /> </FrameLayout> <!-- 导航菜单 --> <ListView android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="left" android:background="#f1f1" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" /> </android.support.v4.widget.DrawerLayout></RelativeLayout>
我在主界面就加了一个 textView
抽屉界面写的是空的 listView 运行效果如下