android - 在工具栏中更改边距和重力折叠标题

标签 android toolbar android-toolbar

我尝试使用带有 2 个图标和 RelativeLayer 的自定义工具栏,并附加 CollapsingToolbarLayout 以折叠标题和图像,但标题文本覆盖在右侧图标和填充上不起作用

 <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar_toolbar"
        android:layout_height="192dp"
        android:layout_width="match_parent">
    <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:expandedTitleMarginStart="64dp"
            app:collapsedTitleTextAppearance="@style/ExpandedAppBar"
            app:paddingStart="10dp"
            app:paddingEnd="10dp"
            app:contentScrim="#ffffff"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
        <ImageView
                android:src="@drawable/about_img"
                app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"/>
        <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                app:layout_collapseMode="pin"
                android:background="#ffffff"
                app:backgroundTint="#ffffff"
                app:contentInsetLeft="0dp"
                app:contentInsetStart="0dp"
                android:layout_height="50dp"
                app:layout_scrollFlags="scroll|enterAlways">
            <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" android:paddingRight="10dip"
                    android:gravity="center_vertical" android:paddingLeft="10dip" android:id="@+id/re_toolbar">
                <ImageView
                        android:layout_width="20dip"
                        android:layout_height="wrap_content"
                        android:id="@+id/img_menu" android:src="@drawable/menu_icon" android:layout_alignParentRight="true"
                        android:paddingTop="10dip"/>
                <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/img_header_info"
                        android:src="@drawable/ic_header_info" android:layout_alignParentLeft="true"
                        android:paddingTop="5dip"/>
            </RelativeLayout>
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

样式文件:

    <style name="CollapsedAppBar" parent="@android:style/TextAppearance">
    <item name="android:textSize">15sp</item>
    <item name="android:textStyle">normal</item>

但我无法更改文本边距和文本对齐方式。

需要 margin :

title need margin title need margin

需要对齐:

enter image description here

和依赖:

compile 'com.android.support:appcompat-v7:22.+'
compile 'com.android.support:design:22.+'
compile 'com.android.support:recyclerview-v7:22.+'
compile 'com.android.support:cardview-v7:22.+'

最佳答案

尝试使用水平方向的 LinearLayout 而不是 RelativeLayout。 将两个 ImageView 放在 LinearLayout 中。 LinearLayout 不允许其子项相互重叠。
您还可以添加标题作为工具栏的标题。
试试这个方法:

getSupportActionBar().setTitle("Your Title");

关于android - 在工具栏中更改边距和重力折叠标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36111377/

相关文章:

Android隐藏的标题栏回来了

swift - nstoolbaritem 大小 OS X

android - 折叠工具栏标题消失

android - android中的圆形按钮..避免按下 "outside"按钮?

android - 具有透明度的 TextureView

android - 使用稀松布或颜色叠加更改 CollapsingToolbarLayout 中标题的背景颜色

android - RecycleView 由于工具栏而在底部留下空白

android - 如何使用 Android 中的折叠工具栏布局使此个人资料图片移动

android - 第一个 Android 应用程序 : R cannot be resolved to a variable?

java - 基本工具栏使应用程序崩溃