Android darkview中没有MaterialCardView背景
我最近从Android darkview中没有MaterialCardView背景,android,android-layout,android-cardview,material-components-android,Android,Android Layout,Android Cardview,Material Components Android,我最近从material-1.0转到1.2(1.1也有问题!) 现在,在亮视图中一切正常,但在暗视图中,没有卡片背景。请参阅所附图片以了解我的意思。 我附上我的java和xml文件,以便您可以检查 main活动 public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { private Context mContex
material-1.0
转到1.2
(1.1也有问题!)
现在,在亮视图中一切正常,但在暗视图中,没有卡片背景。请参阅所附图片以了解我的意思。
我附上我的java和xml文件,以便您可以检查
main活动
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;
}
}
}
活动\u 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>
<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>
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>
<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>
将更新更改的xml文件的结果,并添加一个屏幕截图。请检查。使用
而不是
android:background="?attr/colorPrimary"
缺少活动\u main。请编辑您的问题事实上,我认为这不是必需的,cardView在app_bar_main中,这是给定的。你真的认为这是必要的吗?如果这样说的话,这个问题的长度会非常大,因为在MaterialCardView
中使用android:theme=“@style/theme.MaterialComponents.DayNight”
没有意义。同时删除android:background=“?attr/colorPrimary”
并使用app:cardBackgroundColor
。更改卡片的背景色就足够了。@GabrieleMariotti:我说的是青色(mycolorPrimary
),它位于卡片周围的浅色(底部),而不是深色(顶部))。我会做的事情和张贴的update@BaRud在任何情况下,青色与卡片无关。卡的默认背景色基于colorsface
或由cardBackgroundColor
属性覆盖。在暗模式下,背景色顶部有一个将改变卡本身背景(即textview部件的背景)的区域,而不是卡周围的暗区域。@BaRud不清楚您在问什么android:background
在cardView中是错误的。由于您的标题在darkview中不是MaterialCardView背景,我认为您需要更改卡片背景。无论如何,您可以根据主题在中更改父布局的背景。ie:if(theme.equals(“黑暗”){parent_layout.setBackground(“你的背景”)}