简体   繁体   English

如何在 Android 中将 RatingBar 居中

[英]How to center a RatingBar in Android

I have created a custom RatingBar as described in one of the tutorials given here on stack-overflow and it's looking great, the problem is with the centering of the image.我已经创建了一个自定义 RatingBar,如此处给出的关于堆栈溢出的教程之一所述,它看起来很棒,问题在于图像的居中。 here is what I've got:这是我所拥有的:在此处输入图片说明

As you can see the TextView is centered vertically in the LinearLayout, but the RatingBar is at the top of it.如您所见,TextView 在 LinearLayout 中垂直居中,但 RatingBar 位于其顶部。 Here is my xml code of this image:这是我的这张图片的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:foregroundGravity="center" >

    <LinearLayout
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:gravity="center" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <RatingBar
            android:id="@+id/ratingBar1"
            style="@style/starsRatingBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:numStars="5"
            android:rating="2.3"
            android:stepSize="1" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:src="@drawable/ic_launcher" />

    </LinearLayout>

</ScrollView>

Here is the code of the styling:这是样式的代码:

<style name="starsRatingBar" parent="@android:style/Widget.RatingBar">
        <item name="android:progressDrawable">@drawable/star_ratingbar_full</item>
        <item name="android:minHeight">36dip</item>
        <item name="android:maxHeight">36dip</item>
    </style>

Any ideas in the RatingBar would be apprisiated! RatingBar 中的任何想法都会得到认可!

THANKS谢谢

Ok, I think I've got it.好的,我想我明白了。

The key issue is the ScrollView : it automatically makes things compact, unless you specifically tell it otherwise.关键问题是ScrollView :它会自动使事情变得紧凑,除非您另外特别说明。 In the scroll view, set在滚动视图中,设置

android:fillViewport="true"

to make it use the space available.使其使用可用空间。

Hello to get all items at top you have to put:您好,要将所有物品放在顶部,您必须放置:

<TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:text="TextView" />

instead of:代替:

  <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

if you just want the rating bar centred: just remove style="@style/starsRatingBar"如果您只想将评分栏居中:只需删除 style="@style/starsRatingBar"

EDIT: forget any think I said befor, it seems to work better when I changed the style to : (adjust your min/max height to get "perfect result" )编辑:忘记我之前说过的任何想法,当我将样式更改为 :(调整您的最小/最大高度以获得“完美结果”时,它似乎工作得更好)

<style name="starsRatingBar" parent="@android:style/Widget.RatingBar">
        <item name="android:progressDrawable">@android:drawable/star_on</item>
        <item name="android:minHeight">26dip</item>
        <item name="android:maxHeight">26dip</item>
    </style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM