在 Cardview 中使用评级栏

Use Rating Bar in Cardview

我必须想象一组卡片视图,每个卡片视图都必须有一个带有五颗星的评级栏。 我使用了下面的代码:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="@color/primary">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/card_title"
        android:text="@string/titolo"
        android:lines="1"
        android:textColor="@color/icons"
        android:textSize="15sp"
        />



    <android.widget.RatingBar
        android:id="@+id/myRatingBar"
        style="?android:attr/ratingBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:isIndicator="true"
        android:background="@android:color/holo_red_dark"/>

</LinearLayout>

但我在我的卡片视图中看不到警告栏。问题出在哪里? 请帮助我,谢谢

 <LinearLayout 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:orientation="horizontal">

        <TextView
            android:id="@+id/card_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:lines="1"
            android:text="titolo"
            android:textColor="#123456"
            android:textSize="15sp" />


        <android.widget.RatingBar
            android:id="@+id/myRatingBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:isIndicator="true"
            android:numStars="5" />

    </LinearLayout>

这是因为宽度 match_parent,当您将方向设置为 match_parent 时,它占用了整个屏幕宽度。如上修改。

LinearLayout 方向更改为垂直。

您的布局问题在于您将 LinearLayout 方向设置为水平。您的 TextView 设置为 match_parent,然后您添加了 RatingBar。所以它会跑到屏幕外面。

将您的方向更改为垂直并尝试。