【问题标题】:CollapsingToolbarLayout toolbar is in the status barCollapsingToolbarLayout 工具栏在状态栏中
【发布时间】:2016-01-20 05:58:03
【问题描述】:

我按照本教程了解如何实现折叠工具栏布局Link Here
我成功地得到了我想要的结果。
经过一些调试后,我发现我的操作栏搜索按钮位于应该在图像内部的图像上方。
我正在做的是将搜索图标放在图像中,就像任何其他折叠工具栏布局一样。
并且图像应该占据顶部(colorPrimaryDark)。

这是我的 XML。(基于给定链接上的教程)

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:background="#FAFAFA">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbarLayout"
        android:layout_width="match_parent"
        android:layout_height="270dp"

        >

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="28dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/img_content"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/gameover2"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />

            <include
                android:id="@+id/app_bar"
                layout="@layout/app_bar" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingTop="16dp">

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="16dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:paddingBottom="24dp"
                    >

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="5dp"
                        android:gravity="center"
                        android:text="@string/coordinatar_card"
                        android:textAppearance="@style/TextAppearance.AppCompat.Title"
                        android:textIsSelectable="false"
                        android:textSize="25sp" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/coordinatar_card_desc"
                        android:textSize="18sp" />

                </LinearLayout>

            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="16dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:paddingTop="24dp">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="5dp"
                        android:gravity="center"
                        android:text="@string/appBarLayout_card"
                        android:textAppearance="@style/TextAppearance.AppCompat.Title"
                        android:textIsSelectable="false"
                        android:textSize="25sp" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/appBarLayout_card_desc"
                        android:textSize="18sp" />

                </LinearLayout>

            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="16dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:paddingTop="24dp">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="5dp"
                        android:gravity="center"
                        android:text="@string/collapsing_toolbar_card"
                        android:textAppearance="@style/TextAppearance.AppCompat.Title"
                        android:textIsSelectable="false"
                        android:textSize="25sp" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/collapsing_toolbar_card_desc"
                        android:textSize="18sp" />

                </LinearLayout>

            </android.support.v7.widget.CardView>

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:clickable="true"
        android:src="@drawable/ic_search"
        app:layout_anchor="@+id/appbarLayout"
        app:layout_anchorGravity="bottom|right|end" />

</android.support.design.widget.CoordinatorLayout>

还有我的活动..

public class selectedPOI extends AppCompatActivity {

    private RecyclerView recyclerView;
    private JuanAdapter adapter;
    CollapsingToolbarLayout collapsingToolbar;
    int mutedColor = R.attr.colorPrimary;
    FloatingActionButton fab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selected_poi);
        Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
        collapsingToolbar.setTitle("Collapsing Toolbar");
        fab=(FloatingActionButton)findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v, "You clicked on the fab", Snackbar.LENGTH_SHORT).show();
            }
        });


    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_search, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            NavUtils.navigateUpFromSameTask(this);
        }
        return super.onOptionsItemSelected(item);
    }


}

编辑:
在 API 21 或更高版本上,ActionBar 位于图像上方。
但在低于 API21 的 API 中,它工作正常。

【问题讨论】:

  • 如果您使用过 v21,请向我们展示您的风格
  • 资源> 试图删除 windowstranslucent,它解决了问题,但我希望图片适合顶部屏幕。

标签: android android-layout material-design android-collapsingtoolbarlayout


【解决方案1】:

好的,我认为您的这个问题可以有两种解决方案。 1. 您可以在 v21 样式中将其添加到您的样式&lt;item name="android:fitsSystemWindows"&gt;true&lt;/item&gt;。这将允许您的应用的每个屏幕仅调整到适合您的应用的区域。

  1. 其他方式可以通过代码,试试这个

    toolbar.setPadding(0,getStatusBarHeight(this), 0, 0); 这里的工具栏是你的折叠工具栏。将此检查用于棒棒糖,如果是,则将顶部填充添加到工具栏,与状态栏的高度相同。

public static int getStatusBarHeight(Context ctx) {
        int result = 0;
        int resourceId = ctx.getResources().getIdentifier("status_bar_height", "dimen","android");


        if (resourceId > 0) {
            result = ctx.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

让我们知道,进展如何,我认为它应该为你做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-18
    • 1970-01-01
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多