[英]Android Change Hamburger icon position based on ToolBar size
我试图将“工具栏”中“汉堡包”图标的位置设置为“垂直居中”,但无法这样做。
我根据布局权重确定了ToolBar的高度。 由于客户要求,我无法使用工具栏默认高度。
以下是我的主要活动XML:
<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:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.5"
app:titleTextColor="@color/vodafone_white"
android:background="@mipmap/img_appbar">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<android.support.v4.widget.Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
/>
<TextView
android:id="@+id/toolbar_title"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:textColor="@color/vodafone_white"
android:textSize="35sp"
android:textStyle="bold"
android:gravity="left|center_vertical"
android:text="@string/title_Vodafone" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
<!--Contents-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8.5"
android:background="@color/vodafone_lightGray">
<android.support.v4.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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<!-- Layout for content is here. This can be a RelativeLayout -->
<android.support.design.widget.CoordinatorLayout 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/app_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.keybs.vodafoneqatar.views.MainActivity">
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.keybs.vodafoneqatar.views.MainActivity">
</android.support.constraint.ConstraintLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main2"
app:menu="@menu/activity_main2_drawer" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
</LinearLayout>
有什么办法可以:
要么使此ActionBarDrawerToggle
按钮垂直居中,并留有一些左边距,以使图标适合白色区域的中心。
还是要创建一个替代此ActionBarDrawerToggle
按钮的自定义控件?
我是Android开发的新手,需要帮助。
用于垂直装饰Hamburger Icon
使用工具栏上的minHeight
属性。
例
<android.support.v7.widget.Toolbar
android:minHeight="100dp" //your value
</android.support.v7.widget.Toolbar>
访问Margin | padding
Margin | padding
在与工具栏的左/右侧:
使用contentInset
例:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setContentInsetsAbsolute(toolbar.getContentInsetLeft(), 200); //here 200 is set for padding left
尝试这个
ViewGroup.LayoutParams layoutParams = mToolbar.getLayoutParams();
for (int i = 0; i < mToolbar.getChildCount(); i++) {
if (mToolbar.getChildAt(i) instanceof ImageButton) {
Toolbar.LayoutParams lp = (Toolbar.LayoutParams) mToolbar.getChildAt(i).getLayoutParams();
layoutParams.gravity = Gravity.CENTER_VERTICAL;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.