더 작은 RatingBar를 만드는 방법?
레이아웃에 RatingBar 를 추가했습니다 .
<RatingBar
android:id="@+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0"
/>
그러나 등급 표시 줄의 기본 스타일이 너무 큽니다.
안드로이드 스타일을 추가하여 변경하려고했습니다.style="?android:attr/ratingBarStyleSmall"
그러나 결과가 너무 작아이 속성으로 요금을 설정하는 것은 불가능합니다.
어떻게해야합니까?
기본 RatingBar 위젯은 일종의 절름발이입니다.
소스는 이미 친숙한 " ?android:attr/ratingBarStyleIndicator
" 스타일 외에도 " " 스타일을 참조합니다 ?android:attr/ratingBarStyleSmall
. ratingBarStyleIndicator
약간 작지만 여전히 추악하고 의견에 따르면 이러한 스타일은 "상호 작용을 지원하지 않습니다".
당신은 아마 당신 자신을 굴리는 것이 더 나을 것입니다. http://kozyr.zydako.net/2010/05/23/pretty-ratingbar/ 에는 괜찮은 가이드가 있습니다. (나는 아직 직접하지 않았지만 하루 정도면 시도 할 것입니다.)
행운을 빕니다!
추신 : 죄송합니다, 당신이 찌를 수있는 소스에 대한 링크를 게시하려고했지만 새로운 사용자이고 하나 이상의 URL을 게시 할 수 없습니다. 소스 트리를 통해 길을 찾으면 frameworks / base / core / java / android / widget / RatingBar.java에 있습니다.
여기에 주어진 코드를 붙이는 방법 ...
1 단계. 당신은 자신의 등급 별이 필요합니다 res/drawable
...
2 단계 다음과 같이 res/drawable
필요합니다 ratingstars.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_empty" />
<item android:id="@android:id/secondaryProgress"
android:drawable="@drawable/star_empty" />
<item android:id="@android:id/progress"
android:drawable="@drawable/star" />
</layer-list>
3 단계 다음과 같이 res/values
필요합니다 styles.xml
...
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="foodRatingBar" parent="@android:style/Widget.RatingBar">
<item name="android:progressDrawable">@drawable/ratingstars</item>
<item name="android:minHeight">22dip</item>
<item name="android:maxHeight">22dip</item>
</style>
</resources>
4 단계 레이아웃에서 ...
<RatingBar
android:id="@+id/rtbProductRating"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:numStars="5"
android:rating="3.5"
android:isIndicator="false"
style="@style/foodRatingBar"
/>
그냥 사용하십시오 :
android:scaleX="0.5"
android:scaleY="0.5"
필요에 따라 스케일 팩터를 변경하십시오.
왼쪽에 패딩을 만들지 않고 크기를 조정하려면
android:transformPivotX="0dp"
에 리스너를 추가 할 필요가 없습니다 ?android:attr/ratingBarStyleSmall
. 추가 만하면 android:isIndicator=false
클릭 이벤트가 캡처됩니다. 예 :
<RatingBar
android:id="@+id/myRatingBar"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:isIndicator="false" />
OS가 구현 한 작은 것
<RatingBar
android:id="@+id/ratingBar"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
이것을 적용하십시오.
<RatingBar android:id="@+id/indicator_ratingbar"
style="?android:attr/ratingBarStyleIndicator"
android:layout_marginLeft="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
위의 솔루션에서 생각한 것보다 쉽고 쉬운 솔루션을 찾았습니다. 작은 등급 표시 줄을 만든 다음 onTouchListener를 추가했습니다. 거기에서 클릭의 너비를 계산하고 그 별 수를 결정합니다. 이것을 여러 번 사용한 결과, 내가 찾은 유일한 단점은 LinearLayout으로 묶지 않으면 작은 등급 표시 줄의 그림이 테이블에서 항상 올바르게 나오지 않는다는 것입니다 (편집기에서는 보이지만 장치는 아닙니다) . 어쨌든 내 레이아웃에서 :
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RatingBar
android:id="@+id/myRatingBar"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5" />
</LinearLayout>
내 활동 내에서 :
final RatingBar minimumRating = (RatingBar)findViewById(R.id.myRatingBar);
minimumRating.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View view, MotionEvent event)
{
float touchPositionX = event.getX();
float width = minimumRating.getWidth();
float starsf = (touchPositionX / width) * 5.0f;
int stars = (int)starsf + 1;
minimumRating.setRating(stars);
return true;
}
});
I hope this helps someone else. Definitely easier than drawing one's own (although I've found that also works, but I just wanted an easy use of stars).
<RatingBar
android:id="@+id/rating_bar"
style="@style/Widget.AppCompat.RatingBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Use This Code
<RatingBar
android:id="@+id/ratingBarSmalloverall"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="30dp"
android:isIndicator="false"
android:numStars="5" />
<RatingBar
android:id="@+id/ratingBar1"
android:numStars="5"
android:stepSize=".5"
android:rating="3.5"
style="@android:style/Widget.DeviceDefault.RatingBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
although answer of Farry works, for Samsung devices RatingBar took random blue color instead of the defined by me. So use
style="?attr/ratingBarStyleSmall"
instead.
Full code how to use it:
<android.support.v7.widget.AppCompatRatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?attr/ratingBarStyleSmall" // use smaller version of icons
android:theme="@style/RatingBar"
android:rating="0"
tools:rating="5"/>
<style name="RatingBar" parent="Theme.AppCompat">
<item name="colorControlNormal">@color/grey</item>
<item name="colorControlActivated">@color/yellow</item>
<item name="android:numStars">5</item>
<item name="android:stepSize">1</item>
</style>
The Best Answer for small ratingbar
<RatingBar
android:layout_width="wrap_content"
style = "?android:attr/ratingBarStyleSmall"
android:layout_height="wrap_content" />
use the following code for small rating bar with onTouch event
enter code here
<RatingBar
android:id="@+id/ratingBar"
style="?android:ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="false"
android:rating="4"
android:stepSize="0.5" />
The best answer I got
<style name="MyRatingBar" parent="@android:style/Widget.RatingBar">
<item name="android:minHeight">15dp</item>
<item name="android:maxHeight">15dp</item>
<item name="colorControlNormal">@color/white</item>
<item name="colorControlActivated">@color/home_add</item>
</style>
user like this
<RatingBar
style="?android:attr/ratingBarStyleIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="false"
android:max="5"
android:rating="3"
android:scaleX=".8"
android:scaleY=".8"
android:theme="@style/MyRatingBar" />
Increase/Decrease sizes by using scaleX and scaleY value
<RatingBar
android:rating="3.5"
android:stepSize="0.5"
android:numStars="5"
style = "?android:attr/ratingBarStyleSmall"
android:theme="@style/RatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
// if you want to style
<style name="RatingBar" parent="Theme.AppCompat">
<item name="colorControlNormal">@color/colorPrimary</item>
<item name="colorControlActivated">@color/colorAccent</item>
</style>
// add these line for small rating bar
style = "?android:attr/ratingBarStyleSmall"
After a lot of research I found the best solution to reduce the size of custom rating bars is to get the size of progress drawable in certain sizes as mentioned below :
xxhdpi - 48*48 px
xhdpi - 36*36 px
hdpi - 24*24 px
그리고 스타일에서 모든 해상도에 대해 최소 높이와 최대 높이를 16dp로 설정하십시오.
이 크기가 ratingbar.small 스타일 속성과 매우 호환되며 동등한 크기로 최상의 소형 등급 막대를 얻는 데 도움이되지 않는지 알려주십시오.
그것은 등급 표시 줄 xml style = "? android : attr / ratingBarStyleSmall"에서 나를 위해 일하고 있습니다.
이 문제를 다음과 같이 해결합니다. style = "? android : attr / ratingBarStyleSmall"
참고 URL : https://stackoverflow.com/questions/2874537/how-to-make-a-smaller-ratingbar
'IT' 카테고리의 다른 글
여러 값과 같은 MySQL (0) | 2020.07.03 |
---|---|
mongo-서버 127.0.0.1:27017에 연결할 수 없습니다 (0) | 2020.07.03 |
최대 절전 모드 주석-필드 또는 속성 액세스 중 어떤 것이 더 좋습니까? (0) | 2020.07.03 |
리스트를 대략 같은 길이의 N 개 부분으로 나누기 (0) | 2020.07.03 |
BadValue 유효하지 않거나 사용자 로캘이 설정되지 않았습니다. (0) | 2020.07.03 |