【问题标题】:AppCompat Toolbar elevation missing (with RecyclerView) despite setting properties尽管设置了属性,但 AppCompat 工具栏高度缺失(使用 RecyclerView)
【发布时间】:2019-04-08 22:41:22
【问题描述】:

出于某种原因,我的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();
    }
}

【问题讨论】:

    标签: java android xml android-fragments android-toolbar


    【解决方案1】:

    您的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 属性。

    【讨论】:

    • 移除封装Toolbar的LinearLayout,并为其添加背景。
    • 我的 bacground 已经以编程方式设置了背景,因此这也不起作用
    • 你确定吗?您发布的代码中没有对背景的引用。此外,Toolbar mMasterToolbar = findViewById(R.id.toolbar_1line); 这一行不指向活动布局中的任何视图。
    • 刚刚修正了我代码中的错别字。是的,我敢肯定。这太奇怪了。
    • 令人惊讶的是,该选项确实如此。谢谢。
    【解决方案2】:

    删除活动 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">
    
        <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>
    

    【讨论】:

    • 我认为这可能是正确的答案。包裹 Toolbar 的 LinearLayout 可能会裁剪阴影,因为默认情况下会在 视图边界之外绘制阴影。
    • 出于某种原因,这仍然不能解决问题。这很奇怪。
    • 另外,将所有用于创建工具栏的代码从 onStart 移动到 onCreate。并删除 onStart 中重复的 setContentView。你只需要一次。并删除以编程方式将所有颜色/高度设置为工具栏。您应该只需要在 xml 文件中设置一次。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多