Android實(shí)現(xiàn)仿網(wǎng)易新聞主界面設(shè)計(jì)
下面先來一張效果圖
根據(jù)圖片分析,要實(shí)現(xiàn)的有側(cè)邊欄DrawerLayout,ActionBar的顏色和菜單以及ActionBarDrawerToggle的動畫效果.
在這之前,Theme要改成帶有ActionBar的主題
android:theme="@android:style/Theme.Holo.Light"
一:側(cè)邊欄-DrawerLayout
根據(jù)官方文檔,DrawerLayout布局的第一個(gè)視圖是activity的主視圖,第二個(gè)是側(cè)邊欄視圖
因此主布局可以如下這樣
FrameLayout為主視圖,include加載的則為左側(cè)邊欄,因此是start屬性
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mdrawlaout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffeeeeee">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="這是主界面"/>
</FrameLayout>
<include
android:layout_width="180dp"
android:layout_height="match_parent"
layout="@layout/drawer_layout_left"
android:layout_gravity="start"
android:clickable="true"
/>
</android.support.v4.widget.DrawerLayout>
左側(cè)邊欄的實(shí)現(xiàn),這里用到一個(gè)開源項(xiàng)目circleimageview,可以設(shè)置圓形頭像,很簡單的使用.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
android:background="#ffffff">
<!-- 圓形頭像-->
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/circleimageview"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginLeft="45dp"
android:layout_marginTop="30dp"
android:src="@drawable/circlel_header"
app:border_color="#ff0000"
app:border_width="2dp" />
<!-- 菜單列表-->
<TextView
android:id="@+id/tv_item1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:gravity="center"
android:text="夜間工具"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_item2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="繪畫工具"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_item3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="測試1"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_item4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="測試2"
android:textSize="16sp" />
</LinearLayout>
二:ActionBar的配置
ActionBar配置主要用代碼進(jìn)行配置
//取得ActionBar
actionBar = getActionBar();
//設(shè)置不顯示標(biāo)題
actionBar.setDisplayShowTitleEnabled(false);
//設(shè)置使用logo
actionBar.setDisplayUseLogoEnabled(true);
//設(shè)置logo
actionBar.setLogo(R.drawable.netease_top);
//設(shè)置ActionBar背景
Drawable background = getResources().getDrawable(R.drawable.top_bar_background);
actionBar.setBackgroundDrawable(background);
//設(shè)置是將應(yīng)用程序圖標(biāo)轉(zhuǎn)變成可點(diǎn)擊圖標(biāo),并添加返回按鈕
actionBar.setDisplayHomeAsUpEnabled(true);
其中背景色主要通過xml文件進(jìn)行配置
R.drawable.top_bar_background
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="@color/top_title_bar_normal_backgrond_color"/> </shape>
top_title_bar_normal_backgrond_color
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="red">#ff0000</color> <color name="gray">#bbbbbb</color> <color name="black">#000000</color> <color name="top_title_bar_normal_backgrond_color">#EB413D</color> <color name="top_title_bar_button_press_background_color">#D83C38</color> </resources>
這樣就能顯示紅色的了
三:menu菜單的設(shè)置
這里主要是修改menu_main.xml這個(gè)文件來設(shè)置,解析的話,as自動生成的onCreateOptionsMenu(Menu menu)會自動解析
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id="@+id/action_settings"
android:icon="@drawable/night_biz_pc_menu_icon"
android:orderInCategory="1"
android:title="@string/app_name"
android:showAsAction="always"/>
<item
android:id="@+id/action_btn01"
android:icon="@drawable/abc_ic_menu_moreoverflow_mtrl_alpha"
android:orderInCategory="2"
android:title="更多"
android:showAsAction="always">
<menu>
<item
android:id="@+id/action_btn02"
android:icon="@drawable/biz_plugin_manage_weather"
android:orderInCategory="100"
android:title="11/13"
android:showAsAction="never"/>
<item
android:id="@+id/action_btn03"
android:icon="@drawable/biz_plugin_manage_offline"
android:orderInCategory="100"
android:title="離線"
android:showAsAction="never"/>
<item
android:id="@+id/action_btn04"
android:icon="@drawable/biz_plugin_manage_theme"
android:orderInCategory="100"
android:title="夜間"
android:showAsAction="never"/>
<item
android:id="@+id/action_btn05"
android:icon="@drawable/biz_plugin_manage_search"
android:orderInCategory="100"
android:title="搜索"
android:showAsAction="never"/>
<item
android:id="@+id/action_btn06"
android:icon="@drawable/biz_plugin_manage_qrcode"
android:orderInCategory="100"
android:title="掃一掃"
android:showAsAction="never"/>
<item
android:id="@+id/action_btn07"
android:icon="@drawable/biz_plugin_manage_offline"
android:orderInCategory="100"
android:title="設(shè)置"
android:showAsAction="never"/>
</menu>
</item>
</menu>
四:ActionBarDrawerToggle的實(shí)現(xiàn)
ActionBarDrawerToggle是一個(gè)開關(guān),用于打開/關(guān)閉DrawerLayout抽屜,ActionBarDrawerToggle 提供了一個(gè)方便的方式來配合DrawerLayout和ActionBar,以實(shí)現(xiàn)推薦的抽屜功能。即點(diǎn)擊ActionBar的home按鈕,即可彈出DrawerLayout抽屜。
在Activity中的兩個(gè)回調(diào)函數(shù)中使用它:
- onConfigurationChanged
- onOptionsItemSelected
調(diào)用ActionBarDrawerToggle.syncState() 在Activity的onPostCreate()中;指示,ActionBarDrawerToggle與DrawerLayout的狀態(tài)同步,并將ActionBarDrawerToggle中的drawer圖標(biāo),設(shè)置為ActionBar的Home-Button的icon
//設(shè)置DrawerLayout的點(diǎn)擊事件
mdrawlayout.setDrawerListener(new MyAPPdrawerlistener());
//設(shè)置抽屜開關(guān)
mActionBarDrawerToggle = new ActionBarDrawerToggle(
this,this.mdrawlayout,R.string.drawer_open,R.string.drawer_close);
/**
* 該方法會自動和actionBar關(guān)聯(lián), 將開關(guān)的圖片顯示在了action上
* 如果不設(shè)置,也可以有抽屜的效果,不過是默認(rèn)的圖標(biāo)
* @param savedInstanceState
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mActionBarDrawerToggle.syncState();
}
/**
* 當(dāng)設(shè)備配置改變的時(shí)候
* @param newConfig
*/
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mActionBarDrawerToggle.onConfigurationChanged(newConfig);
}
/**
* 菜單點(diǎn)擊事件
* @param item
* @return
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
//添加mActionBarDrawerToggle點(diǎn)擊效果
return mActionBarDrawerToggle.onOptionsItemSelected(item)||super.onOptionsItemSelected(item);
}
最后在DrawerLayout的點(diǎn)擊事件中配置ActionBarDrawerToggle跟隨抽屜來改變即可
private class MyAPPdrawerlistener implements DrawerLayout.DrawerListener{
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
mActionBarDrawerToggle.onDrawerSlide(drawerView, slideOffset);
}
@Override
public void onDrawerOpened(View drawerView) {
mActionBarDrawerToggle.onDrawerOpened(drawerView);
}
@Override
public void onDrawerClosed(View drawerView) {
mActionBarDrawerToggle.onDrawerClosed(drawerView);
}
@Override
public void onDrawerStateChanged(int newState) {
mActionBarDrawerToggle.onDrawerStateChanged(newState);
}
}
備注一些用到的知識:
1.android:paddingLeft和android:layout_marginLeft區(qū)別:區(qū)別是android:layout_marginLef是設(shè)置整個(gè)布局離左邊的距離,android:paddingLeft是設(shè)置布局里面的內(nèi)容距離左邊
2.android:gravity:設(shè)置的是控件自身上面的內(nèi)容位置,android:layout_gravity:設(shè)置控件本身相對于父控件的顯示位置
3.android:orderInCategory="1",actionbar里每個(gè)item的優(yōu)先級,值越大優(yōu)先級越低,actionbar地方不夠就會放到overflow中
4.android studio導(dǎo)入github的項(xiàng)目的時(shí)候,直接把庫復(fù)制到原代碼下,然后在grade中添加作者給的引入語句即可.
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
- Android項(xiàng)目實(shí)戰(zhàn)之仿網(wǎng)易新聞的頁面(RecyclerView )
- Android實(shí)現(xiàn)仿網(wǎng)易新聞的頂部導(dǎo)航指示器
- Android實(shí)現(xiàn)網(wǎng)易新聞客戶端首頁效果
- Android實(shí)現(xiàn)類似網(wǎng)易新聞選項(xiàng)卡動態(tài)滑動效果
- Android 仿網(wǎng)易新聞客戶端分類排序功能
- Android模擬實(shí)現(xiàn)網(wǎng)易新聞客戶端
- Android實(shí)現(xiàn)網(wǎng)易新聞客戶端側(cè)滑菜單(1)
- Android組件DrawerLayout仿網(wǎng)易新聞v4.4側(cè)滑菜單
- Android實(shí)現(xiàn)網(wǎng)易新聞客戶端側(cè)滑菜單(2)
- Android仿網(wǎng)易新聞圖片詳情下滑隱藏效果示例代碼
相關(guān)文章
Android TextView實(shí)現(xiàn)跑馬燈效果的方法
這篇文章主要介紹了Android TextView跑馬燈效果實(shí)現(xiàn)方法,涉及Android布局文件中相關(guān)屬性的設(shè)置技巧,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01
Android利用屬性動畫實(shí)現(xiàn)優(yōu)酷菜單
這篇文章主要為大家詳細(xì)介紹了Android利用屬性動畫實(shí)現(xiàn)優(yōu)酷菜單,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
Android開發(fā)OkHttp執(zhí)行流程源碼分析
這篇文章主要為大家介紹了Android開發(fā)OkHttp執(zhí)行流程源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android實(shí)現(xiàn)添加商品到購物車動畫效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)添加商品到購物車的動畫效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Android實(shí)踐之帶加載效果的下拉刷新上拉加載更多
這篇文章主要給大家介紹了關(guān)于Android實(shí)踐之下拉刷新上拉加載更多的相關(guān)資料,實(shí)現(xiàn)的效果在現(xiàn)在的很多項(xiàng)目中都能用到,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12

