要使用toolBar,首先我們需要導包:
implementation 'com.android.support:appcompat-v7:26.1.0'
我們看下截圖:
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICMxQDOzUDMzITMwYDM4EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
我們需要在res/value/style.xml中設定
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
接着在res/layout/toolbar_title.xml中加入Toolbar控件.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimaryDark"
app:layout_scrollFlags="scroll|enterAlways">
<TextView
android:id="@+id/toolbar_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:layout_gravity="center_horizontal"
android:textColor="#ffffff"
android:textSize="20sp" />
<TextView
android:id="@+id/toolbar_add"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical|right"
android:padding="10dp"
android:visibility="gone"
android:textColor="#ffffff"
android:textSize="20sp" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
再來看看ToolbarActivity的代碼:
public class ToolbarActivity extends AppCompatActivity {
protected Context mContext;
private Toolbar toolbar;
private NestedScrollView nestedScrollView;
private TextView toolbar_name;
public TextView toolbar_add;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.toolbar_title);
initToolbarView();
initToolbarData();
}
public void initToolbarView() {
mContext = this;
toolbar_name = findViewById(R.id.toolbar_name);
toolbar = findViewById(R.id.toolbar_toolbar);
nestedScrollView = findViewById(R.id.nestedScrollView);
toolbar_add = findViewById(R.id.toolbar_add);
}
public void initToolbarData() {
setSupportActionBar(toolbar);
setTitle("");
//顯示傳回按鈕
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//更改傳回按鈕的圖示顔色和樣式
final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_material);
upArrow.setColorFilter(getResources().getColor(R.color.ffffff), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);
}
//設定标題名字
protected void setToolbarName(String name) {
toolbar_name.setText(name);
}
protected void setAddChildView(int childView) {
View view = LayoutInflater.from(this).inflate(childView, null);
// LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
// view.setLayoutParams(layoutParams);
nestedScrollView.addView(view);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}
}
接這樣了,。一個最簡單的toolbar,。