I think you can refer to the following sample (the activity_main.xml looks like the one I posted in your previous question, but now edited - adding buttons inside top toolbar)
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/button1a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:src="@drawable/ic_action_cloud" />
<ImageButton
android:id="@+id/button2a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:src="@drawable/ic_action_copy" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_bottom"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentBottom="true"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/button1"
android:layout_width="10dp"
android:layout_height="wrap_content"
android:layout_weight="0.175"
android:background="@android:color/transparent"
android:src="@drawable/ic_action_back" />
<View
android:id="@+id/view1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0.1" />
<ImageButton
android:id="@+id/button2"
android:layout_width="10dp"
android:layout_height="wrap_content"
android:layout_weight="0.175"
android:background="@android:color/transparent"
android:src="@drawable/ic_action_cloud" />
<View
android:id="@+id/view2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0.1"
android:background="@android:color/transparent" />
<ImageButton
android:id="@+id/button3"
android:layout_width="10dp"
android:layout_height="wrap_content"
android:layout_weight="0.175"
android:background="@android:color/transparent"
android:src="@drawable/ic_action_copy" />
<View
android:id="@+id/view3"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0.1"
android:background="@android:color/transparent" />
<ImageButton
android:id="@+id/button4"
android:layout_width="10dp"
android:layout_height="wrap_content"
android:layout_weight="0.175"
android:background="@android:color/transparent"
android:src="@drawable/ic_action_help" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
</RelativeLayout>
Then in your activity:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private final Context mContext = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ImageButton button1 = (ImageButton) findViewById(R.id.button1);
button1.setOnClickListener(this);
}
@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) {
// 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) {
Toast.makeText(mContext, "action_settings", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
Toast.makeText(mContext, "button1", Toast.LENGTH_SHORT).show();
break;
default:
}
}
}
Hope this helps!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…