我在我的应用程序中使用约束布局和物料卡视图。当我在读取设备中运行应用程序时,它显示正确并且工作正常(第一张图片),但是当我在android studio虚拟设备和genymotion设备约束布局childs上运行它时得到不正确的位置,卡片视图显示为黑色视图! (第二张图片)。 请帮助我。我很困惑! 布局XML代码为:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="@drawable/main_back_8"
tools:context=".MainActivity">
<!-- Exit -->
<com.google.android.material.card.MaterialCardView
android:id="@+id/menu_exit_card"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="16dp"
android:orientation="vertical"
app:cardCornerRadius="@dimen/menu_item_corner"
app:layout_constraintBottom_toTopOf="@+id/ad_frame"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="@+id/menu_connect_us_card"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.7"
android:adjustViewBounds="true"
android:padding="@dimen/dimen_x_small"
android:src="@drawable/ico_exit"
android:contentDescription="@string/exit"/>
<custom_views.vajenameh.BoldTextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:background="@color/menu_back_color"
android:gravity="center"
android:text="@string/exit"
android:textColor="#ffffff" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/menu_exit"
android:background="@drawable/menu_item_back"
android:clickable="true"
android:focusable="true"
android:onClick="menuClick"
/>
</com.google.android.material.card.MaterialCardView>
<!-- Connect us-->
<com.google.android.material.card.MaterialCardView
android:id="@+id/menu_connect_us_card"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="16dp"
android:orientation="vertical"
app:cardCornerRadius="@dimen/menu_item_corner"
app:layout_constraintBottom_toBottomOf="@+id/menu_exit_card"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="@+id/menu_settings_card"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/menu_exit_card"
app:layout_constraintTop_toTopOf="@+id/menu_exit_card">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.7"
android:adjustViewBounds="true"
android:padding="@dimen/dimen_x_small"
android:src="@drawable/ico_connect_us"
android:contentDescription="@string/connect_us"/>
<custom_views.vajenameh.BoldTextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:background="@color/menu_back_color"
android:gravity="center"
android:text="@string/connect_us"
android:textColor="#ffffff" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/menu_connect_us"
android:background="@drawable/menu_item_back"
android:clickable="true"
android:focusable="true"
android:onClick="menuClick"
/>
</com.google.android.material.card.MaterialCardView>
<!-- Settings -->
<com.google.android.material.card.MaterialCardView
android:id="@+id/menu_settings_card"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="16dp"
android:orientation="vertical"
app:cardCornerRadius="@dimen/menu_item_corner"
app:layout_constraintBottom_toBottomOf="@+id/menu_connect_us_card"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/menu_connect_us_card"
app:layout_constraintTop_toTopOf="@+id/menu_connect_us_card">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.7"
android:adjustViewBounds="true"
android:padding="@dimen/dimen_x_small"
android:src="@drawable/ico_settings"
android:contentDescription="@string/settings"/>
<custom_views.vajenameh.BoldTextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:background="@color/menu_back_color"
android:gravity="center"
android:text="@string/settings"
android:textColor="#ffffff" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/menu_settings"
android:background="@drawable/menu_item_back"
android:clickable="true"
android:focusable="true"
android:onClick="menuClick"
/>
</com.google.android.material.card.MaterialCardView>
<!-- Favorite -->
<com.google.android.material.card.MaterialCardView
android:id="@+id/menu_favor_card"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="16dp"
android:background="@color/colorAccent"
android:orientation="vertical"
app:cardCornerRadius="@dimen/menu_item_corner"
app:layout_constraintBottom_toTopOf="@+id/menu_exit_card"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="@+id/menu_words_index_card"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.7"
android:adjustViewBounds="true"
android:padding="@dimen/dimen_x_small"
android:src="@drawable/ico_favor"
android:contentDescription="@string/favorites"/>
<custom_views.vajenameh.BoldTextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:background="@color/menu_back_color"
android:gravity="center"
android:text="@string/favorites"
android:textColor="#ffffff" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/menu_favor"
android:background="@drawable/menu_item_back"
android:clickable="true"
android:focusable="true"
android:onClick="menuClick"
/>
</com.google.android.material.card.MaterialCardView>
<!-- Words index -->
<com.google.android.material.card.MaterialCardView
android:id="@+id/menu_words_index_card"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="16dp"
android:background="@color/colorAccent"
android:orientation="vertical"
app:cardCornerRadius="@dimen/menu_item_corner"
app:layout_constraintBottom_toBottomOf="@+id/menu_favor_card"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="@+id/menu_search_card"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/menu_favor_card"
app:layout_constraintTop_toTopOf="@+id/menu_favor_card">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.7"
android:adjustViewBounds="true"
android:padding="@dimen/dimen_x_small"
android:src="@drawable/ico_index"
android:contentDescription="@string/index"/>
<custom_views.vajenameh.BoldTextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:background="@color/menu_back_color"
android:gravity="center"
android:text="@string/index"
android:textColor="#ffffff" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/menu_words_index"
android:background="@drawable/menu_item_back"
android:clickable="true"
android:focusable="true"
android:onClick="menuClick"
/>
</com.google.android.material.card.MaterialCardView>
<!-- Search -->
<com.google.android.material.card.MaterialCardView
android:id="@+id/menu_search_card"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="16dp"
android:background="@color/colorAccent"
android:orientation="vertical"
app:cardCornerRadius="@dimen/menu_item_corner"
app:layout_constraintBottom_toBottomOf="@+id/menu_words_index_card"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/menu_words_index_card"
app:layout_constraintTop_toTopOf="@+id/menu_words_index_card">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.7"
android:adjustViewBounds="true"
android:padding="@dimen/dimen_x_small"
android:src="@drawable/ico_search"
android:contentDescription="@string/search"/>
<custom_views.vajenameh.BoldTextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:background="@color/menu_back_color"
android:gravity="center"
android:text="@string/search"
android:textColor="#ffffff" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/menu_search"
android:background="@drawable/menu_item_back"
android:clickable="true"
android:focusable="true"
android:onClick="menuClick"
/>
</com.google.android.material.card.MaterialCardView>
<FrameLayout
android:id="@+id/ad_frame"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:minHeight="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
</FrameLayout>
<!-- Day word panel -->
<com.google.android.material.card.MaterialCardView
android:id="@+id/day_word_panel"
android:layout_width="0dp"
android:visibility="gone"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="24dp"
android:orientation="vertical"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toTopOf="@+id/menu_favor_card"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/header">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="end"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="TODO"
android:src="@android:drawable/ic_delete" />
<custom_views.vajenameh.BoldTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/day_word"
android:textColor="#212121"
android:textSize="@dimen/text_size_header" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<custom_views.vajenameh.BoldTextView
android:id="@+id/day_word_word_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/word_word_test"
android:textColor="#212121"
android:textSize="@dimen/text_size_sub_header" />
<custom_views.vajenameh.NormalTextView
android:id="@+id/day_word_meaning_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/word_meaning_test"
android:textColor="#212121"
android:textSize="@dimen/text_size_explain" />
</LinearLayout>
</ScrollView>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="@+id/header"
android:visibility="gone"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:cardCornerRadius="0dp"
app:cardBackgroundColor="@color/menu_back_color"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:orientation="horizontal">
<ImageView
android:id="@+id/main_updateBtn_iv"
android:layout_width="@dimen/ic_dimen_small"
android:layout_height="@dimen/ic_dimen_small"
android:layout_gravity="center_vertical"
android:layout_margin="@dimen/dimen_small"
android:src="@drawable/ico_update"
android:contentDescription="TODO" />
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"></FrameLayout>
<custom_views.vajenameh.BoldTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="@dimen/dimen_small"
android:text="@string/header_title"
android:textColor="#FFFFFF"
android:textSize="@dimen/text_size_header" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
<ImageView
android:id="@+id/boldTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:adjustViewBounds="true"
android:gravity="center"
android:src="@drawable/blackboard_5"
app:layout_constraintBottom_toTopOf="@+id/menu_favor_card"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>