首页 > 解决方案 > 尽管设置了属性,但 AppCompat 工具栏高度缺失(使用 RecyclerView)

问题描述

出于某种原因,我Toolbar不会为海拔投下阴影。我什至尝试过使用Java,但它仍然无法正常工作。

关于为什么这没有按预期执行的任何想法?我的应用不支持 pre-Lollipop(最低 API 为 24)。

活动 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/masterToolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize">

        <include layout="@layout/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"/>
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/master_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

工具栏 XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/my_toolbar"
    android:elevation="4dp"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    android:minHeight="?android:attr/actionBarSize">

    <LinearLayout
        android:id="@+id/mytoolbar_textlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="vertical">

        <TextView
            android:id="@+id/mytoolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@android:style/TextAppearance.Material.Widget.ActionBar.Title"/>
    </LinearLayout>
</android.support.v7.widget.Toolbar>

活动课

public class MyActivity extends AppCompatActivity {

    private Boolean mCurrentValue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.md);
    }

    @Override
    protected void onStart() {
        super.onStart();

        setContentView(R.layout.md);

        SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        Boolean mNewValue = mSharedPreferences.getBoolean("my_preference", false);

        if (mCurrentValue != mNewValue) {
            recreate();
        }

        // ... do other stuff here
        Resources r = getBaseContext().getResources();
        int fourDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, r.getDisplayMetrics());

        final String toolbarColour = "#EE7C0E";

        Toolbar mMasterToolbar = findViewById(R.id.my_toolbar);
        setSupportActionBar(mMasterToolbar);
        mMasterToolbar.setBackgroundColor(Color.parseColor(toolbarColour));
        mMasterToolbar.setElevation(fourDp);
        mMasterToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });


        final TextView mTitle = this.findViewById(R.id.toolbar_1line_title);
        mTitle.setText(getString(R.string.london_overground));

        FragmentMain newFragment = new FragmentMain();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.master_container, newFragment);
        transaction.commit();
    }
}

标签: javaandroidxmlandroid-fragmentsandroid-toolbar

解决方案


android:background在你的Toolbar.

例子:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/my_toolbar"
    android:elevation="4dp"
    android:background="?attr/colorPrimary"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    android:minHeight="?android:attr/actionBarSize">

注意:为了将高程应用到任何视图,它需要有背景。


编辑: 由于您的应用具有最小 SDK 24,您可以使用该translationZ属性。


推荐阅读