使用toobar實(shí)現(xiàn)知乎首頁樣式(新特性)

使用toobar實(shí)現(xiàn)知乎首頁樣式(新特性

效果

圖片來自http://www.itdecent.cn/p/79604c3ddcae

ToolBar簡(jiǎn)介


ToolBar是Android

5.0推出的一個(gè)新的導(dǎo)航控件用于取代之前的ActionBar,由于其高度的可定制性、靈活性、具有MaterialDesign風(fēng)格等優(yōu)點(diǎn),越來越多的應(yīng)用也用上了ToolBar,比如常用的知乎軟件其頂部導(dǎo)航欄正是使用ToolBar。官方考慮到仍有一部分用戶的手機(jī)版本號(hào)低于5.0,所以,ToolBar也放進(jìn)了supportv7包內(nèi),使得低版本的系統(tǒng)也能使用上ToolBar。本文將使用supportv7支持包的ToolBar來進(jìn)行講解,包括其基本用法、樣式定制等知識(shí)點(diǎn)。


使用方式

1,引入support v7支持包

在你項(xiàng)目的build.gradle內(nèi)輸入如下代碼,即能引入支持包,該支持包內(nèi)有能向下兼容的ToolBar:

dependencies {

compile fileTree(dir: 'libs', include: ['*.jar'])

compile 'com.android.support:appcompat-v7:23.1.1'

}

2,在布局中添加

`

android:id="@+id/toolbar"

android:layout_width="match_parent"

android:layout_height="?attr/actionBarSize"

android:background="?attr/colorPrimary">

`

3,在activity進(jìn)行設(shè)置

public class ToolbarActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_toolbar);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

getSupportActionBar().setDisplayShowTitleEnabled(false);

toolbar.setTitle("主標(biāo)題");

toolbar.setTitleTextColor(getResources().getColor(R.color.colorPrimaryDark));

toolbar.setSubtitle("副標(biāo)題");

toolbar.setLogo(R.mipmap.ic_launcher);

toolbar.setOnLongClickListener(new View.OnLongClickListener() {

@Override

public boolean onLongClick(View v) {

Toast.makeText(ToolbarActivity.this, "logo", Toast.LENGTH_SHORT).show();

return false;

}

});

toolbar.setNavigationIcon(android.R.drawable.ic_input_delete);

toolbar.setNavigationOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Toast.makeText(ToolbarActivity.this, "圖標(biāo)點(diǎn)擊", Toast.LENGTH_SHORT).show();

}

});

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.menu_main, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {

case R.id.action_edit:

Toast.makeText(this, "查找按鈕", Toast.LENGTH_SHORT).show();

break;

case R.id.action_share:

Toast.makeText(this, "分享按鈕", Toast.LENGTH_SHORT).show();

break;

}

return false;

}

}

具體實(shí)現(xiàn)請(qǐng)?jiān)L問giithub鏈接https://github.com/yxwandroid/AndroidSummary/blob/master/ToolBar5/app/src/main/java/willson/activity/ToolbarActivity.java

參考

http://blog.csdn.net/a553181867/article/details/51336899

http://www.itdecent.cn/p/79604c3ddcae

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容