我正在嘗試將動態卡片視圖添加到水平滾動視圖中,以便我擁有與資料庫中的行一樣多的卡片視圖。我在一個單獨的 XML 檔案中定義了 cardview。
但是我無法將卡片視圖添加到我的主要活動的布局中,因為卡片視圖為空。從不同的檔案添加元素有問題嗎?我應該在代碼中定義卡片視圖嗎?或者我應該改用 RecyclerView 嗎?
我搜索了很多,但沒有任何幫助。
這是我的主要活動。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout scroll = findViewById(R.id.layout_horizontal_theme);
CardView cardView = findViewById(R.id.theme_cardview);
scroll.addView(cardView);
}
用于卡片視圖的 XML 檔案
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_marginBottom="20dp"
android:contentDescription="@string/card"
android:minHeight="150dp"
app:cardBackgroundColor="#FF402D"
app:cardCornerRadius="16dp"
android:id="@ id/theme_cardview">
<!-- app:cardElevation="10dp"-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:id="@ id/layout_theme_card">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/card_image"
android:cropToPadding="true"
android:maxWidth="100dp"
android:src="@drawable/android_developer" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:contentDescription="@string/card_theme"
android:text="@string/card_theme"
android:textColor="@color/Not_so_white_white"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</androidx.cardview.widget.CardView>
和主要活動的 XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:layout_gravity="center">
<RelativeLayout
android:id="@ id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:layout_constraintBottom_toTopOf="@ id/horizontalScrollView"
app:layout_constraintEnd_toEndOf="@id/guideline2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="@id/guideline"
app:layout_constraintTop_toTopOf="parent">
<include layout="@layout/progress_bar" />
</RelativeLayout>
<HorizontalScrollView
android:id="@ id/horizontalScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/list_scroll"
android:fillViewport="true"
app:layout_constraintBottom_toTopOf="@ id/horizontalScrollView2"
app:layout_constraintEnd_toEndOf="@id/guideline2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="@id/guideline"
app:layout_constraintTop_toBottomOf="@ id/relativeLayout">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@ id/layout_horizontal_diff">
<include layout="@layout/difficulty_card" />
<include layout="@layout/difficulty_card" />
<include layout="@layout/difficulty_card" />
<include layout="@layout/difficulty_card" />
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView
android:id="@ id/horizontalScrollView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/list_scroll_2"
android:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/horizontalScrollView"
app:layout_constraintVertical_bias="0.503">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@ id/layout_horizontal_theme">
<!-- <include layout="@layout/theme_card" />
<include layout="@layout/theme_card" />
<include layout="@layout/theme_card" />
<include layout="@layout/theme_card" />-->
</LinearLayout>
</HorizontalScrollView>
<androidx.constraintlayout.widget.Guideline
android:id="@ id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.10" />
<androidx.constraintlayout.widget.Guideline
android:id="@ id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.90" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="@ id/navDrawer"
android:layout_height="match_parent"
android:layout_width="wrap_content"
app:headerLayout="@layout/navigation_drawer"
app:menu="@menu/nav_drawer_menu"
android:layout_gravity="start"
android:fitsSystemWindows="true"/>
</androidx.drawerlayout.widget.DrawerLayout>
感謝大家的幫助:)
uj5u.com熱心網友回復:
您需要擴充您的卡片視圖,然后您可以將其用于任何目的:
View view = View.inflate(this, R.layout.cardview_layout, null);
然后您可以將其轉換為 CardView
CardView cardView = (CardView) View.inflate(this, R.layout.cardview_layout, null);
您的代碼將如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout scroll = findViewById(R.id.layout_horizontal_theme);
CardView cardView = (CardView) View.inflate(this, R.layout.cardview_layout, null);
scroll.addView(cardView);
}
uj5u.com熱心網友回復:
按照示例創建自定義視圖。之后,創建您的自定義視圖并將它們置于水平視圖。
CardView cardView = findViewById(R.id.theme_cardview);總是回傳空值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/448695.html
