android rating bar xml and numStars

北慕城南 提交于 2020-01-11 10:00:09

问题


I'm a beginner in designing with XML :D This is the XML of my rating bar and friends :

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="2" >

    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:textColor="#336699"
        android:textSize="18dp"
        android:textStyle="bold" />

    <RatingBar
        android:id="@+id/rtbHighScore"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:numStars="5"
        android:max="100"
        android:rating="0.0"
        android:stepSize="0.0"
        style="?android:attr/ratingBarStyleSmall"         
        android:layout_weight="1" />

    <ImageView
        android:id="@+id/imgRow"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:layout_marginBottom="5dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="5dp"
        android:gravity="center_vertical"
        android:src="@drawable/rightarrow" />
</LinearLayout>

The android:numStars is 5, but this is what i get :

I read that the layout_width must be wrap_content so the rating bar stars will follow my android:numStars and this is what i get when i change it to wrap_content :

I want to create 5stars rating bar in the right of my textview(or the left of my right arrow image) Thanks :D


回答1:


Use this Layout file for proper 5 stars.

<TextView
    android:id="@+id/txtTitle"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:layout_weight="1"
    android:gravity="center_vertical"
    android:textColor="#336699"
    android:textSize="18dp"
    android:textStyle="bold" />

<RatingBar
    android:id="@+id/rtbHighScore"
    style="?android:attr/ratingBarStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_marginRight="45dp"
    android:max="5"
    android:numStars="5"
    android:rating="0.0"
    android:stepSize="0.0" />

<ImageView
    android:id="@+id/imgRow"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:layout_marginBottom="5dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="5dp"
    android:gravity="center_vertical"
    />

I think android:gravity="center_vertical" was the problem.




回答2:


Okay, i have an idea to use a relative layout and i also use a little drag-drop :

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="2" >

    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:gravity="center_vertical"
        android:textColor="#336699"
        android:textSize="18dp"
        android:textStyle="bold" 
        android:layout_weight="1"
        />

    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <RatingBar
            android:id="@+id/rtbHighScore"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="15dp"
            android:layout_marginTop="15dp"
            android:layout_marginRight="45dp"
            android:numStars="5"
            android:max="100"
            android:rating="0.0"
            android:stepSize="0.0"
            style="?android:attr/ratingBarStyleSmall"
            />

        <ImageView
            android:id="@+id/imgRow"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="96dp"
            android:src="@drawable/rightarrow" />

    </RelativeLayout>
</LinearLayout>

And this code make my layout "perfect"



来源:https://stackoverflow.com/questions/12724376/android-rating-bar-xml-and-numstars

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!