[英]How to make all activities(except MainActivity) have ActionBar/Toolbar at once with the title of the Activity and a back button?
[英]Android How to make back button without Actionbar and Toolbar?
我在res / values / style.xml中制作了这样的主题,并应用于删除ActionBar。
<style name="AppTheme.NoActionbar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">false</item>
</style>
我使LinerLayout看起来像带有标题和按钮的ActionBar(它将具有后退功能)。
我想上传图片,但是还不能上传。
这是layout.xml的一部分
<LinearLayout
android:id="@+id/ActionBarProductInfo"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="@color/colorPrimary"
android:orientation="horizontal">
<ImageButton
android:id="@+id/backToMain"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:background="@drawable/image_back_34dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="210dp"
android:layout_marginStart="210dp"
android:text="Input product information"
android:textColor="@android:color/white"
android:textSize="40sp"
android:textStyle="bold" />
</LinearLayout>
如何在按钮(id:backToMain)中添加后退功能?
只需将您的ImageButton的ID绑定到Java中,然后在该ID上设置clicklistner,就像这样
ImageButton back = (ImageButton)findViewById(R.id.backToMain);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
您只需要在活动中包括布局,然后绑定后退按钮即可。
<include layout="@layout/layout" />
然后将此活动
ImageButton back1 = (ImageButton) findViewById(R.id.backToMain);
back1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
首先初始化工具栏
setSupportActionBar(toolbar);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
处理点击
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getItemId() == android.R.id.home) {
Timber.d("Home pressed");
}
return super.onOptionsItemSelected(menuItem);
}
希望这对您有用。
如下所示修改您的布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/ActionBarProductInfo"
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="@color/colorPrimary"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton
android:id="@+id/backToMain"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginLeft="15dp"
android:background="@mipmap/ic_back" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:text="Input product information"
android:textColor="@android:color/white"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.