android垂直评级栏大小问题

时间:2015-01-27 10:55:31

标签: android size vertical-alignment android-styles ratingbar

我已经垂直安排了评级栏,但是这样做,我在屏幕上管理所需尺寸的评级栏时遇到问题。我尝试使用图像和默认评级栏进行自定义。但他们都没有帮助我。

到目前为止,我所做的工作如下:

1)Styles.xml

<style name="foodRatingBar" parent="@android:style/Widget.RatingBar">
        <item name="android:progressDrawable">@drawable/star_rating_bar_full</item>
        <item name="android:minHeight">48dip</item>
        <item name="android:maxHeight">48dip</item>
    </style>

2)star_rating_bar_full.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+android:id/background"
          android:drawable="@drawable/star_ratingbar_full_empty" />
    <item android:id="@+android:id/secondaryProgress"
          android:drawable="@drawable/star_ratingbar_full_empty" />
    <item android:id="@+android:id/progress"
          android:drawable="@drawable/star_ratingbar_full_filled" />
</layer-list>

3)star_ratingbar_full_empty.xml

<?xml version="1.0" encoding="utf-8"?>

<!-- This is the rating bar drawable that is used to
 show a filled cookie. -->
<selector
xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true"
      android:state_window_focused="true"
      android:drawable="@drawable/star2" />

<item android:state_focused="true"
      android:state_window_focused="true"
      android:drawable="@drawable/star2" />

<item android:state_selected="true"
      android:state_window_focused="true"
      android:drawable="@drawable/star2" />

<item android:drawable="@drawable/star2" />

</selector>

4)star_ratingbar_full_filled.xml

<?xml version="1.0" encoding="utf-8"?>

 <!-- This is the rating bar drawable that is used to
 show a unfilled cookie. -->
<selector
xmlns:android="http://schemas.android.com/apk/res/android">



<item android:state_focused="true"
      android:state_window_focused="true"
      android:drawable="@drawable/star1" />

<item android:state_selected="true"
      android:state_window_focused="true"
      android:drawable="@drawable/star1" />

<item android:drawable="@drawable/star1" />

</selector>

5)VerticalRatingBar.java

package com.example.ratingbarwidget;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.RatingBar;

public class VerticalRatingBar extends RatingBar {
    private int x, y, z, w;

    @Override
    protected void drawableStateChanged() {
        // TODO Auto-generated method stub
        super.drawableStateChanged();
    }

    public VerticalRatingBar(Context context) {
        super(context);
    }

    public VerticalRatingBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public VerticalRatingBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldh, oldw);
        this.x = w;
        this.y = h;
        this.z = oldw;
        this.w = oldh;
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec,
            int heightMeasureSpec) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    protected void onDraw(Canvas c) {
        c.rotate(-90);
        c.translate(-getHeight(), 0);
        super.onDraw(c);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isEnabled()) {
            return false;
        }

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

            setSelected(true);
            setPressed(true);
            break;
        case MotionEvent.ACTION_MOVE:
            setProgress(getMax()
                    - (int) (getMax() * event.getY() / getHeight()));
            onSizeChanged(getWidth(), getHeight(), 0, 0);

            break;
        case MotionEvent.ACTION_UP:
            setSelected(false);
            setPressed(false);
            break;

        case MotionEvent.ACTION_CANCEL:
            break;
        }
        return true;
    }

    @Override
    public synchronized void setProgress(int progress) {

        if (progress >= 0)
            super.setProgress(progress);

        else
            super.setProgress(0);
        onSizeChanged(x, y, z, w);

    }
}

6)图像/图标如下:

star-on image from first pair star-off image from first pair star-on image from second pair enter image description here

7)布局,我使用带有样式的评级栏

    <com.navrideclient.widgets.VerticalRatingBar     
    style="@style/foodRatingBar"       
    android:id="@+id/ratingBar_car"  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"  
    android:numStars="5"  
    android:stepSize="1.0" />

0 个答案:

没有答案