如何自动调整用作图层列表中可绘制项目的图像高度?

时间:2016-01-25 12:12:41

标签: android android-layout height autoresize ratingbar

我有RatingBar的自定义图片,我想根据父版面留下的空间自动调整图像高度。

这是布局:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/rateus_bg"
    android:scaleType="fitXY" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.ims.westmont.android.view.TextViewUniSans
        android:id="@+id/textview_rate_us"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="@string/rate_us"
        android:textAllCaps="true"
        android:textColor="@android:color/white"
        android:textSize="60sp"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">

        <RatingBar
            android:id="@+id/ratingbar_rate"
            style="@style/rateUsBar"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:numStars="5"
            android:rating="0"
            android:stepSize="0.5" />
    </LinearLayout>

    <com.ims.westmont.android.view.TextViewUniSans
        android:id="@+id/button_send"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/send"
        android:textColor="@android:color/white"
        android:textSize="60sp"
        android:textAllCaps="true"
        android:layout_weight="1"
        android:gravity="center"/>
</LinearLayout>

这是视觉表现: rate

由于我使用的是LinearLayout,因此其中的三个元素具有相同的高度。 RatingBar使用自定义样式:

<style name="rateUsBar" parent="@android:style/Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/rating_stars</item>
    <item name="android:minHeight">90dp</item>
    <item name="android:maxHeight">90dp</item>
</style>

最后是可绘制的rating_stars

<?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/rate_empty" />
    <item
        android:id="@android:id/secondaryProgress"
        android:drawable="@drawable/rate_empty" />
    <item
        android:id="@android:id/progress"
        android:drawable="@drawable/rate_filled" />
</layer-list>

这是一个重量问题还是我应该创建一个自定义视图(类)来调整高度?

2 个答案:

答案 0 :(得分:2)

一个解决方案是

使用不同的值文件夹

  1. 值 2.values-mdpi,values-mdpi-land 3 values-sw320dp,values-sw320dp-land 4 values-sw480dp,values-sw480dp-land 5 values-sw600dp,values-sw600dp-land 6 values-sw720dp,values-sw720dp-土地
  2. 现在将style.xml文件放在每个文件夹上,并根据您的设备分别设置高度和宽度。

     <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical">
    
            <RatingBar
                android:id="@+id/ratingbar_rate"
                style="@style/rateUsBar"
                android:numStars="5"
                android:rating="0"
                android:stepSize="0.5" />
        </LinearLayout>
    

    您可以在预览选项中检查预览不同设备的布局。

答案 1 :(得分:1)

如果您在使用下面的代码时遇到任何问题,请告诉我。 通过以下代码,您可以使用任何值设置高度和宽度:)

将其放入customratingbar.java

package com.example.ashis.myapplication;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.ashis.myapplication.R;


/**
 * Created by poliveira on 07/08/2014.
 */

/**
 * regular rating bar. it wraps the stars making its size fit the parent
 */
public class customratingbar extends LinearLayout {
    public IRatingBarCallbacks getOnScoreChanged() {
        return onScoreChanged;
    }

    public void setOnScoreChanged(IRatingBarCallbacks onScoreChanged) {
        this.onScoreChanged = onScoreChanged;
    }

    public interface IRatingBarCallbacks {
        void scoreChanged(float score);
    }

    private int mMaxStars = 5;
    private float mCurrentScore = 2.5f;
    private int mStarOnResource = R.mipmap.stars;
    private int mStarOffResource =  R.mipmap.ic_launcheroff;
    private int mStarHalfResource = R.mipmap.stars;
    private ImageView[] mStarsViews;
    private float mStarPadding;
    private IRatingBarCallbacks onScoreChanged;
    private int mLastStarId;
    private boolean mOnlyForDisplay;
    private double mLastX;
    private boolean mHalfStars = true;

    public customratingbar(Context context) {
        super(context);
        init();
    }

    public float getScore() {
        return mCurrentScore;
    }

    public void setScore(float score) {
        score = Math.round(score * 2) / 2.0f;
        if (!mHalfStars)
            score = Math.round(score);
        mCurrentScore = score;
        refreshStars();
    }

    public void setScrollToSelect(boolean enabled) {
        mOnlyForDisplay = !enabled;
    }

    public customratingbar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initializeAttributes(attrs, context);
        init();
    }

    private void initializeAttributes(AttributeSet attrs, Context context) {
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.CustomRatingBar);
        final int N = a.getIndexCount();
        for (int i = 0; i < N; ++i) {
            int attr = a.getIndex(i);
            if (attr == R.styleable.CustomRatingBar_maxStars)
                mMaxStars = a.getInt(attr, 5);
            else if (attr == R.styleable.CustomRatingBar_stars)
                mCurrentScore = a.getFloat(attr, 2.5f);
            else if (attr == R.styleable.CustomRatingBar_starHalf)
                mStarHalfResource = a.getResourceId(attr, android.R.drawable.star_on);
            else if (attr == R.styleable.CustomRatingBar_starOn)
                mStarOnResource = a.getResourceId(attr, android.R.drawable.star_on);
            else if (attr == R.styleable.CustomRatingBar_starOff)
                mStarOffResource = a.getResourceId(attr, android.R.drawable.star_off);
            else if (attr == R.styleable.CustomRatingBar_starPadding)
                mStarPadding = a.getDimension(attr, 0);
            else if (attr == R.styleable.CustomRatingBar_onlyForDisplay)
                mOnlyForDisplay = a.getBoolean(attr, false);
            else if (attr == R.styleable.CustomRatingBar_halfStars)
                mHalfStars = a.getBoolean(attr, true);
        }
        a.recycle();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public customratingbar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initializeAttributes(attrs, context);
        init();
    }

    void init() {
        mStarsViews = new ImageView[mMaxStars];
        for (int i = 0; i < mMaxStars; i++) {
            ImageView v = createStar();
            addView(v);
            mStarsViews[i] = v;
        }
        refreshStars();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;
    }

    /**
     * hardcore math over here
     *
     * @param x
     * @return
     */
    private float getScoreForPosition(float x) {
        if (mHalfStars)
            return (float) Math.round(((x / ((float) getWidth() / (mMaxStars * 3f))) / 3f) * 2f) / 2;
        float value = (float) Math.round((x / ((float) getWidth() / (mMaxStars))));
        return value < 0 ? 1 : value;
    }

    private int getImageForScore(float score) {
        if (score > 0)
            return Math.round(score) - 1;
        else return -1;
    }

    private void refreshStars() {
        boolean flagHalf = (mCurrentScore != 0 && (mCurrentScore % 0.5 == 0)) && mHalfStars;
        for (int i = 1; i <= mMaxStars; i++) {

            if (i <= mCurrentScore)
                mStarsViews[i - 1].setImageResource(mStarOnResource);
            else {
                if (flagHalf && i - 0.5 <= mCurrentScore)
                    mStarsViews[i - 1].setImageResource(mStarHalfResource);
                else
                    mStarsViews[i - 1].setImageResource(mStarOffResource);
            }
        }
    }

    private ImageView createStar() {
        ImageView v = new ImageView(getContext());
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.weight = 1;
        v.setPadding((int) mStarPadding, 0, (int) mStarPadding, 0);
        v.setAdjustViewBounds(true);
        v.setScaleType(ImageView.ScaleType.FIT_CENTER);
        v.setLayoutParams(params);
        v.setImageResource(mStarOffResource);
        return v;
    }

    private ImageView getImageView(int position) {
        try {
            return mStarsViews[position];
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        if (mOnlyForDisplay)
            return true;
        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                animateStarRelease(getImageView(mLastStarId));
                mLastStarId = -1;
                break;
            case MotionEvent.ACTION_MOVE:
                if (Math.abs(event.getX() - mLastX) > 50)
                    requestDisallowInterceptTouchEvent(true);
                float lastscore = mCurrentScore;
                mCurrentScore = getScoreForPosition(event.getX());
                if (lastscore != mCurrentScore) {
                    animateStarRelease(getImageView(mLastStarId));
                    animateStarPressed(getImageView(getImageForScore(mCurrentScore)));
                    mLastStarId = getImageForScore(mCurrentScore);
                    refreshStars();
                    if (onScoreChanged != null)
                        onScoreChanged.scoreChanged(mCurrentScore);
                }
                break;
            case MotionEvent.ACTION_DOWN:
                mLastX = event.getX();
                lastscore = mCurrentScore;
                mCurrentScore = getScoreForPosition(event.getX());
                animateStarPressed(getImageView(getImageForScore(mCurrentScore)));
                mLastStarId = getImageForScore(mCurrentScore);
                if (lastscore != mCurrentScore) {
                    refreshStars();
                    if (onScoreChanged != null)
                        onScoreChanged.scoreChanged(mCurrentScore);
                }
        }
        return true;
    }

    private void animateStarPressed(ImageView star) {
        if (star != null)
            ViewCompat.animate(star).scaleX(1.2f).scaleY(1.2f).setDuration(100).start();
    }

    private void animateStarRelease(ImageView star) {
        if (star != null)
            ViewCompat.animate(star).scaleX(1f).scaleY(1f).setDuration(100).start();
    }

    public boolean isHalfStars() {
        return mHalfStars;
    }

    public void setHalfStars(boolean halfStars) {
        mHalfStars = halfStars;
    }
}

并将其放在mainactivity.java

package com.example.ashis.myapplication;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity
{
    customratingbar customratingbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
       customratingbar= (com.example.ashis.myapplication.customratingbar) findViewById(R.id.rating);

        customratingbar.setOnScoreChanged(new customratingbar.IRatingBarCallbacks() {
            @Override
            public void scoreChanged(float score) {
                customratingbar.setScore(score);
            }
        });
       FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}

并将其放在res / values / attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomRatingBar">
        <attr name="maxStars" format="integer"/>
        <attr name="stars" format="float"/>
        <attr name="starOff" format="reference|integer"/>
        <attr name="starOn" format="reference|integer"/>
        <attr name="starHalf" format="reference|integer"/>
        <attr name="starPadding" format="dimension"/>
        <attr name="onlyForDisplay" format="boolean"/>
        <attr name="halfStars" format="boolean"/>
    </declare-styleable>
</resources>

并在res / layout / content_main.xml内部

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main"
    tools:context="com.example.ashis.myapplication.MainActivity">

    <com.example.ashis.myapplication.customratingbar
        android:layout_width="300dp"
android:id="@+id/rating"
        android:layout_height="20dp"

        >


    </com.example.ashis.myapplication.customratingbar>
    <TextView
        android:text="Hello World!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>