在這一節中首先學習了java的頁面佈局,在此基礎之上來進行了編程。
圖片以下:
java
代碼以下:android
** * 使用代碼進行登陸界面的佈局 * @author yuchao */ public class CodeLayoutActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //建立一個最外層的根佈局 LinearLayout rootLayout = new LinearLayout(this); rootLayout.setBackgroundColor(Color.CYAN); //設置線性佈局中子View的擺放爲豎直方向 rootLayout.setOrientation(LinearLayout.VERTICAL); //建立一個根部局的LayoutParams LinearLayout.LayoutParams rootParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); //設置根部局的Margin值 rootParams.setMargins(DensityUtil.dip2px(this,5),DensityUtil.dip2px(this,5),DensityUtil.dip2px(this,5),DensityUtil.dip2px(this,5)); rootParams.gravity = Gravity.CENTER_HORIZONTAL; rootLayout.setLayoutParams(rootParams); //設置最外層根部局佈局的背景顏色 rootLayout.setBackgroundColor(Color.CYAN); //將當前建立的根部局設置給Activity setContentView(rootLayout); //建立一個顯示用戶頭像的ImageView ImageView ivIco = new ImageView(this); ivIco.setImageResource(R.drawable.ico); //將建立的用於顯示頭像的ImageView添加到最外層的線性佈局中 rootLayout.addView(ivIco); //當經過子View進行獲取所在的佈局的時候,rootLayout.addView(ivIco)須要寫在前面,緣由想起來也很清楚,當前的ImageView都尚未添加到父View若是直接經過getLayoutParams()固然會出先空指針異常(能夠查看源碼) LinearLayout.LayoutParams ivParams = (LinearLayout.LayoutParams) ivIco.getLayoutParams(); //給用於顯示頭像的ImageView設置寬高 ivParams.width = DensityUtil.dip2px(this,100f); ivParams.height = DensityUtil.dip2px(this,100f); //設置居中的方式(子View在父View下的擺放方式爲水平居中) ivParams.gravity = Gravity.CENTER_HORIZONTAL; ivParams.setMargins(0,DensityUtil.dip2px(this,100),0,0); ivIco.setLayoutParams(ivParams); //建立水平排列的線性佈局,用於放置帳戶相關的View LinearLayout userCountLayout = new LinearLayout(this); userCountLayout.setOrientation(LinearLayout.HORIZONTAL); //將當前的建立的線性佈局添加到根View rootLayout.addView(userCountLayout); //顯示用戶名的TextView TextView tvShowUser = new TextView(this); tvShowUser.setText("用戶名:"); userCountLayout.addView(tvShowUser); LinearLayout.LayoutParams userCountlayoutParams = (LinearLayout.LayoutParams) tvShowUser.getLayoutParams(); userCountlayoutParams.setMargins(DensityUtil.dip2px(this,20),DensityUtil.dip2px(this,20),DensityUtil.dip2px(this,20),DensityUtil.dip2px(this,20)); //建立用於填寫用戶帳戶的EditText EditText edtUserCount = new EditText(this); userCountLayout.addView(edtUserCount); edtUserCount.setHint("請輸入用戶名"); LinearLayout.LayoutParams edtUserCountlayoutParams = (LinearLayout.LayoutParams) edtUserCount.getLayoutParams(); edtUserCount.setWidth(0); edtUserCountlayoutParams.weight = 1; //父View的Gravity屬性,肯定子View的擺放規則 edtUserCountlayoutParams.gravity = Gravity.CENTER_VERTICAL; //View.setGravity方法至關於 android:gravity="center",該屬性表示對View中內容進行的限定 // edtUserCount.setGravity(Gravity.CENTER); //建立線性佈局用於容納密碼相關的View LinearLayout pwdLayout = new LinearLayout(this); pwdLayout.setGravity(LinearLayout.HORIZONTAL); LinearLayout.LayoutParams pwdLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT); pwdLayout.setLayoutParams(pwdLayoutParams); rootLayout.addView(pwdLayout); //TextView顯示密碼文本 TextView tvShowPwd = new TextView(this); tvShowPwd.setText("密 碼:"); pwdLayout.addView(tvShowPwd); LinearLayout.LayoutParams tvShowPwdParams = (LinearLayout.LayoutParams) tvShowPwd.getLayoutParams(); tvShowPwdParams.setMargins(DensityUtil.dip2px(this,20),DensityUtil.dip2px(this,20),DensityUtil.dip2px(this,20),DensityUtil.dip2px(this,20)); tvShowPwdParams.gravity = Gravity.CENTER_VERTICAL; //EdtText用於輸入密碼 EditText edtPwd = new EditText(this); edtPwd.setHint("請輸入密碼"); pwdLayout.addView(edtPwd); //使用new的方式來獲取 LayoutParams LinearLayout.LayoutParams edtPwdParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); //設置權重 edtPwdParams.weight = 1; edtPwdParams.width = 0; //將佈局參數設置給EdtText edtPwd.setLayoutParams(edtPwdParams); //用於進行登陸的button Button btnLogin = new Button(this); btnLogin.setText("登錄"); rootLayout.addView(btnLogin); LinearLayout.LayoutParams btnLoginParams = (LinearLayout.LayoutParams) btnLogin.getLayoutParams(); //設置Button的大小 btnLoginParams.width = LinearLayout.LayoutParams.WRAP_CONTENT; btnLoginParams.height= LinearLayout.LayoutParams.WRAP_CONTENT; btnLoginParams.gravity = Gravity.CENTER_HORIZONTAL;
全局監聽器
其實只是把每一個建立的監聽器添加入List裏 當SDK裏調用一次的時候 遍歷全部的監聽器 執行每一個監聽器的這個方法就好了
好吧 其實就是觀察者模式啦
1.首先寫一個監聽器的接口編程
public interface testSDKListener { public void test(String str); }
2.寫這個監聽器的實現方法app
public class testSDKListenerImpl { private testSDKListener m_listener; public void setListener(testSDKListener listener) { m_listener = listener; } public void test(String str) { if (m_listener != null) { m_listener.test(str); } } }
3.建立一個List
public class testSDKListenerList { private static testSDKListenerList instance; private List<testSDKListener> listeners; public static testSDKListenerList getInstance() { if (instance == null) { instance = new testSDKListenerList(); } return instance; } private testSDKListenerList() { listeners = new ArrayList<testSDKListener>(); } public void setSDKListener(testSDKListener listener) { if (!listeners.contains(listener) && listener != null) { Log.i("testSDKListenerList", "Add a listener!"); this.listeners.add(listener); } } public void test(String str) { for (testSDKListener listener : listeners) { listener.test(str); } } }
4.在主程序中建立監聽器佈局
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); testSDKListenerList.getInstance().setSDKListener(new testSDKListener() { @Override public void test(String str) { Log.d("Listener.test", "str="+str); } });
5.在SDK中執行監聽器的方法學習
testSDKListenerList.getInstance().test("test success!");
通常來講,Android 默認的狀態欄樣式表現爲黑底白字,若是咱們應用的標題欄背景色也爲黑色,那就能與狀態欄很好地銜接在一塊兒,體驗極佳。反之,若是爲其餘的顏色,整個界面的呈現效果就會大打折扣。優化
幸運的是,Android 4.4 版本開始,系統提供了相應的 API,支持狀態欄全透明化,界面 Content View 能夠延伸到狀態欄上,填充狀態欄背景色。而在 Android 5.0 版本開始,系統在此基礎上作了進一步優化和規範,可以實現動態改變狀態欄背景色,在透明度上默認呈現爲半透明化,可定製化程度更高。this
在此基礎上,最終要作到咱們的應用呈如今 Android 各個系統版本上的效果如圖所示:
設計
關於 Android 4.4 版本開始的狀態欄變化,許多人喜歡稱之爲「沉浸式狀態欄」,但從系統提供的 API 命名上能夠看出,核心詞彙爲 「Translucent」,故準確來說,這種效果又應該稱之爲「透明狀態欄」。知乎上對於這兩種叫法也很有爭議,具體內容可參考話題:爲何在國內會有不少用戶把「透明欄」(Translucent Bars)稱做 「沉浸式頂欄」?。可能對於設計師而言,沉浸式仍是透明式的稱呼有所區別,但對於廣大開發者而言,無足輕重,咱們所關注的應該是如何實現這種效果,並可以很好的兼容到各個版本中。
使用案例分析
res/values/styles 文件中定義基礎主題樣式:
<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar"/> <style name="AppTheme" parent="BaseTheme"> </style>
res/values-v19/styles 文件中定義兼容主題樣式:
<style name="AppTheme" parent="BaseTheme"> <item name="android:windowTranslucentStatus">true</item> </style>
而後在 AndroidManifest.xml 文件中使用全局主題樣式:
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="Samples" android:supportsRtl="true" android:name=".MyApplication" android:theme="@style/AppTheme">
新建一個 layout 佈局文件,單獨定義 toolbar 內容,在應用中的其餘 Activity 界面佈局中使用 include 標籤潛入引用:
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/tb_toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?actionBarSize" android:background="@color/colorPrimary" android:fitsSystemWindows="true" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:title="@string/app_name" app:titleTextColor="@android:color/white"> </android.support.v7.widget.Toolbar>
這裏使用 android:fitsSystemWindows="true" 屬性解決內容試圖向上延伸的問題。實際上,也可使用 android:paddingTop="@dimen/toolbar_padding_top" 的方式解決,toolbar_padding_top 間距爲狀態欄高度,在大多數機器上狀態欄高度爲 25dp,固然也能夠經過代碼動態獲取狀態欄高度並設置到 Toolbar 的 paddingTop 屬性上。須要注意的是,這裏要作兼容判斷,好比在 res/values/dimens.xml 中定義toolbar_padding_top 高度爲 0dp,在 res/values-v19/dimens.xml 中爲 25dp,確保兼容 Android 4.4 如下版本。
基本上,作到這些就可以實現文章開頭處圖中的效果。值得注意的是,有時候若是想在 Android 5.0 及以上版本的系統中也作到全透明效果,或者說狀態欄與導航欄的顏色一致,還能夠作進一步兼容處理,畢竟自 5.0 版本開始,系統對於狀態欄背景色的定製提供了更好的 API。如 res/values-v21/styles.xml 中定義:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="AppTheme" parent="BaseTheme"> <item name="android:colorPrimary">@color/colorPrimary</item> <item name="android:colorPrimaryDark">@color/colorPrimary</item> <item name="android:colorAccent">@color/colorAccent</item> </style>
當用戶長按Activity頁面時,彈出的菜單咱們稱爲上下文菜單。咱們常常在Windows中用鼠標右鍵單擊彈出的菜單就是上下文菜單。
ContextMenu與OptionMenu的區別:
一、OptionMenu對應的是activity,一個activity只能擁有一個選項菜單;
二、ContextMenu對應的是view,每一個view均可以設置上下文菜單;
三、通常狀況下ContextMenu經常使用語ListView或者GridView
實現步驟:
(1)首先給View註冊上下文菜單registerForContextMenu()
this.registerForContextMenu(contextView);
(2)添加上下文菜單的內容onCreateContextMenu()
代碼:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.jiapeng.munedemo.MainActivity" tools:ignore="MergeRootFrame" > <ListView android:id="@+id/mune_list" android:layout_width="wrap_content" android:layout_height="wrap_content" > </ListView> </FrameLayout>
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); showListView(); // 註冊上下文菜單 this.registerForContextMenu(listview); } /** * 加載數據 */ private void showListView() { listview = (ListView) findViewById(R.id.mune_list); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getDate()); listview.setAdapter(adapter); } /** * 建立數據源 * * @return list */ private ArrayList<String> getDate() { ArrayList<String> list = new ArrayList<String>(); for (int i = 0; i < 10; i++) { list.add("菜單" + i); } return list; } /** * 添加上下文菜單的菜單項 */ public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.setHeaderTitle("上下文菜單"); menu.setHeaderIcon(R.drawable.ic_launcher); //加載上下文菜單內容 menu.add(1, 1, 1, "保存"); menu.add(1, 2, 1, "更改"); menu.add(1, 3, 1, "刪除"); super.onCreateContextMenu(menu, v, menuInfo); } /** * 建立單擊事件 */ public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case 1: Toast.makeText(this, "點擊了保存", Toast.LENGTH_SHORT).show(); break; case 2: Toast.makeText(this, "點擊了更改", Toast.LENGTH_SHORT).show(); break; case 3: Toast.makeText(this, "點擊了刪除", Toast.LENGTH_SHORT).show(); break; default: break; } return super.onContextItemSelected(item); }