我有一个自定义创建的工具栏。但是当我运行它时,菜单(toolbar_menu.xml
)上的工具栏与我自定义创建的工具栏重叠。
“我的自定义”工具栏:
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Currency Star"
android:textColor="#FFFFFF"
android:textSize="20dp"/>
</android.support.v7.widget.Toolbar>
我的菜单:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/swapper"
android:icon="@drawable/baseline_swap_vertical_circle_white_18"
android:title="Swap currency"
app:showAsAction="ifRoom"/>
<item android:id="@+id/settings"
android:title="Settings"
app:showAsAction="never"/>
</menu>
我已经在清单和styles.xml
中设置了NoActionBar。
MainActivity.java
Toolbar myToolbar = (Toolbar)findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.toolbar_menu, menu);
return true;
}
答案 0 :(得分:2)
尝试这种方式。 为自定义工具栏设置单独的布局..
<com.google.android.material.appbar.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tool="http://schemas.android.com/tools"
android:background="@null"
android:layout_width="match_parent"
app:elevation="0dp"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentInsetStart="0dp"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/_5sdp"
>
<ImageView
android:id="@+id/ivLogo"
android:layout_width="@dimen/_25sdp"
android:layout_height="@dimen/_25sdp"
android:scaleType="centerCrop"
android:src="@drawable/ic_launcher_background"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:id="@+id/tvHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/black"
android:textSize="@dimen/_12sdp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<ImageView
android:id="@+id/ivUserProfile"
android:layout_width="@dimen/_25sdp"
android:layout_height="@dimen/_25sdp"
android:src="@drawable/user_profile_icon"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.Toolbar>
之后,将此布局包含到活动主体中。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="match_parent">
<include
android:id="@+id/amToolbar"
layout="@layout/app_toolbar"
/>
<FrameLayout
android:id="@+id/activity_main_container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintTop_toBottomOf="@+id/amToolbar"
app:layout_constraintBottom_toTopOf="@id/bottom_navigation"
/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemIconTint="@drawable/bottom_navigation_colors"
app:itemTextColor="@drawable/bottom_navigation_colors"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="@menu/bottom_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
在那之后加上这一行。
Toolbar myToolbar = (Toolbar)findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
确保隐藏默认操作栏,并在此代码中进行更改。
我只提供我的代码。
答案 1 :(得分:2)
您可以做两件事。
1-从工具栏中以XML代码删除TextView
,标题将自动为您设置(只需将其在Java代码中设置为所需的内容即可)< / p>
2-如果要在XML代码中使用TextView
,则需要在Java代码中将标题设置为空,如下所示
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tes);
Toolbar myToolbar = (Toolbar)findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
// Here set the title to empty so that the one set automatically app name will be empty
setTitle("");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.toolbar_menu, menu);
return true;
}
答案 2 :(得分:1)
我发现您对我有同样的问题。这是我的解决方案:
//your custom toolbar
Toolbar toolbar = binding.get().toolBarContainer.findViewById(R.id.toolbar);
//your custom title
TextView mTitle = toolbar.findViewById(R.id.toolbar_title);
mTitle.setText(...);
AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setSupportActionBar(toolbar);
//set displayshowtitle to false to disable title toolbar
activity.getSupportActionBar().setDisplayShowTitleEnabled(false);
//set navigation if you need
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_left));
toolbar.setNavigationOnClickListener((View v) -> onBackClicked());