簡體   English   中英

盡管設置了屬性,但缺少AppCompat工具欄高程(使用RecyclerView)

[英]AppCompat Toolbar elevation missing (with RecyclerView) despite setting properties

由於某些原因,我的Toolbar不會為高程投射陰影。 我什至嘗試使用Java,但仍然無法正常工作。

關於此功能為何未達到預期的任何想法? 我的應用程序不支持棒棒糖之前的版本(最低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();
    }
}

您在Toolbar缺少android:background

例:

<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屬性。

刪除活動xml中的線性布局以及width和height屬性。 您只需要include標簽。

<?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">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <include layout="@layout/toolbar_layout" />
    </android.support.design.widget.AppBarLayout>

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

在您的工具欄xml中,嘗試使用app:elevation,因為您正在使用supportActionBar支持庫。

<?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"
app:elevation="4dp"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:minHeight="?android:attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM