【发布时间】:2021-08-13 14:33:02
【问题描述】:
我正在尝试将我的图像徽标置于自定义工具栏中的中心,但我相信我的汉堡图标正在将图像推到右侧。它在 Android Studio 中看起来不错,但在设备上却不行。我一直在寻找解决此问题的方法,但没有运气。我尝试将图像添加到工具栏容器本身并使用中心重心,但这也不起作用。下面是我的主要活动和工具栏的 xml 代码,以及我的 MainActivity.java 代码。感谢您的帮助。
hamburger icon pushing logo left
//custom_toolbar.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_marginBottom="675dp"
android:background="#315c54"
app:contentInsetLeft="0dp"
android:orientation="vertical"
android:padding="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="49dp"
android:src="@drawable/grocers_logo"
android:layout_gravity = "center_horizontal"
/>
</androidx.appcompat.widget.Toolbar>
<ImageView
android:id="@+id/imageView2"
android:layout_width="29dp"
android:layout_height="35dp"
android:layout_marginTop="12dp"
android:clickable="true"
android:onClick="imageClick"
android:src="@drawable/send"
app:layout_constraintEnd_toEndOf="@+id/toolbar2"
app:layout_constraintHorizontal_bias="0.515"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<!--the root view must be the DrawerLayout-->
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="HardcodedText">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/view_foods"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
tools:context=".MainActivity">
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="350dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:clickable="true"
>
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="323dp"
android:layout_height="687dp"
android:layout_marginStart="16dp"
android:layout_marginTop="33dp"
android:layout_marginEnd="36dp"
android:layout_marginBottom="255dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/list_slidermenu"
app:layout_constraintVertical_bias="0.087" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:background="?attr/colorPrimary"
app:layout_constraintBottom_toTopOf="@+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
//MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Objects.requireNonNull(getSupportActionBar()).setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.custom_toolbar);
ActionBar action = getSupportActionBar();
action.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
Toolbar toolbar=(Toolbar)action.getCustomView().getParent();
toolbar.setContentInsetsAbsolute(0, 0);
toolbar.getContentInsetEnd();
toolbar.setPadding(20, 0, 0, 0);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
switch (tab.getPosition())
{
case 0:
MainActivity.drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
// your logic goes here!
break;
case 1:
MainActivity.drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
// your logic goes here!
break;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
drawerLayout = findViewById(R.id.my_drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.nav_open, R.string.nav_close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
displayFragment_MainActivity();
}
【问题讨论】:
-
我试试看,谢谢!!
-
我不确定你是如何使用这个布局的,但我已经重新排列它并删除了一些不必要的东西,其中一些是可疑的并且可能导致观察到的行为:drive.google.com/file/d/1QKIF95peNn24czuIyJToOICOrhHH8tAJ/… .如果此布局仅适用于
Toolbar,而不是整个Activity布局的开头,那么您可以删除ConstraintLayout。 -
运行时截图,带有替代图像:i.stack.imgur.com/vdfLf.png.
-
没问题。是的,
ActionBar只是Activity中Toolbar的特殊名称,基本上,所以无论您使用默认值还是您自己的,它们都是等效的,但是在提供您自己的东西时定制某些东西要容易得多.关闭默认值通常只需将Activity的主题的parent更改为NoActionBar版本(如果可用)即可。然后只需移动<Toolbar>、findViewById()并用它调用setSupportActionBar(),就可以了。您应该不需要您现在拥有的任何其他ActionBar/Toolbar代码。 -
我明白你的意思,是的,大概就是这样。好的,试一试,谢谢!