Отсутствует высота панели инструментов AppCompat (с RecyclerView), несмотря на заданные свойства - PullRequest
0 голосов
/ 05 ноября 2018

По некоторым причинам, мои 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();
    }
}

Ответы [ 2 ]

0 голосов
/ 05 ноября 2018

Удалите линейный макет, атрибуты ширины и высоты в 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>
0 голосов
/ 05 ноября 2018

Вам не хватает 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">

Примечание: Чтобы применить высоту к любому виду, он должен иметь фон.


EDIT: Поскольку ваше приложение имеет минимальный SDK 24, вы можете использовать атрибут translationZ.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...