Отсутствует фон MaterialCardView в darkview - PullRequest
0 голосов
/ 18 февраля 2020

Я недавно перешел с material-1.0 на 1.2 (проблема с 1.1 тоже есть!) Теперь все хорошо в представлении Light, но в представлении Dark фон карты отсутствует , Смотрите прикрепленные картинки, чтобы понять, что я имею в виду. Dark View Light View

Я прикрепляю свои файлы java и xml, чтобы вы могли проверить их.

MainActivity

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

  private Context mContext;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
    } else {
      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
    }
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    String theme = sharedPref.getString("theme", "Default");
//    Toast.makeText(this, theme, Toast.LENGTH_LONG).show();
    if (theme.equals("Dark")) {
      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    } else if (theme.equals("Light")) {
      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    } else {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
      } else {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
      }
    }

    super.onCreate(savedInstanceState);
    mContext = getApplicationContext();

    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

//    Navigation Drawer
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
        this, drawer, toolbar, R.string.navigation_drawer_open,
        R.string.navigation_drawer_close);
    if (drawer != null) {
      drawer.addDrawerListener(toggle);
    }
    toggle.syncState();

    NavigationView navigationView = findViewById(R.id.nav_view);
    if (navigationView != null) {
      navigationView.setNavigationItemSelectedListener(this);
    }
  }


  @Override
  public boolean onNavigationItemSelected(MenuItem item) {
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    // Handle navigation view item clicks here.
    switch (item.getItemId()) {
      case R.id.nav_manage:
        // Handle the tools action (for now display a toast).
        drawer.closeDrawer(GravityCompat.START);
        Intent intentSetting = new Intent(this, SettingsActivity.class);
        startActivity(intentSetting);
        return true;
      case R.id.about_phocast:
        // Handle the share action (for now display a toast).
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://rudrab.github.io/thePhocast")));
        drawer.closeDrawer(GravityCompat.START);
//        displayToast(getString(R.string.chose_share));
        return true;
      default:
        return false;
    }
  }
}

Activity_main. xml

appbar_main. xml

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity">

  <com.google.android.material.appbar.AppBarLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <com.google.android.material.appbar.MaterialToolbar
      android:id="@+id/toolbar"
      android:layout_width="match_parent"
      android:layout_height="?attr/actionBarSize"
      android:background="?attr/colorPrimary"
      android:minHeight="?attr/actionBarSize"
      android:theme="?attr/actionBarTheme"
      android:title="@string/app_name" />

    <com.google.android.material.card.MaterialCardView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_margin="8dp"
      android:layout_marginStart="10dp"
      android:layout_marginBottom="-2dp"
      android:background="?attr/colorPrimary"
      android:theme="@style/Theme.MaterialComponents.DayNight"
      app:cardCornerRadius="4dp"
      app:cardElevation="4dp">

      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:orientation="vertical">

        <ImageView
          android:layout_width="match_parent"
          android:layout_height="80dp"
          android:contentDescription="@string/cityimg_desc"
          android:scaleType="fitXY"
          app:srcCompat="@drawable/property_image_3" />



          <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/myrect"
            android:elevation="8dp"
            android:paddingStart="@dimen/activity_horizontal_margin"
            android:paddingEnd="0dp"
            android:text="@string/location_not_found"
            android:textColor="#ffffff"
            android:textSize="20sp" />

      </LinearLayout>
    </com.google.android.material.card.MaterialCardView>
  </com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

style.xml = style-night. xml

<resources>

  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
  </style>

  <style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
  </style>

  <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar" />

</resources>

И я не могу узнать, где ошибка.

Пожалуйста, помогите /

Обновление: после сообщений @Soumik и @ Gabriele Моя обновленная тема:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:theme="@style/Theme.MaterialComponents.DayNight"
  tools:context=".MainActivity">

  <com.google.android.material.appbar.AppBarLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <com.google.android.material.appbar.MaterialToolbar
      android:id="@+id/toolbar"
      android:layout_width="match_parent"
      android:layout_height="?attr/actionBarSize"
      android:background="@color/colorPrimary"
      android:minHeight="?attr/actionBarSize"
      android:theme="?attr/actionBarTheme"
      android:title="@string/app_name" />

    <com.google.android.material.card.MaterialCardView
      style="@style/Widget.MaterialComponents.CardView"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_margin="8dp"
      android:layout_marginStart="10dp"
      android:layout_marginBottom="-2dp"
      app:cardCornerRadius="4dp"
      app:cardBackgroundColor="@color/colorPrimary"
      app:cardElevation="4dp">

      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:orientation="vertical">

        <ImageView
          android:layout_width="match_parent"
          android:layout_height="80dp"
          android:contentDescription="@string/cityimg_desc"
          android:scaleType="fitXY"
          app:srcCompat="@drawable/property_image_3" />


        <TextView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@drawable/myrect"
          android:elevation="8dp"
          android:paddingStart="@dimen/activity_horizontal_margin"
          android:paddingEnd="0dp"
          android:text="@string/location_not_found"
          android:textColor="#ffffff"
          android:textSize="20sp" />

      </LinearLayout>
    </com.google.android.material.card.MaterialCardView>
  </com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

enter image description here

Результат измененный файл xml обновляется и добавляется снимок экрана. Пожалуйста, проверьте.

1 Ответ

0 голосов
/ 18 февраля 2020

Используйте

    app:cardBackgroundColor="@color/colorPrimary"

вместо

    android:background="?attr/colorPrimary"
...