gpt4 book ai didi

android - 如何在 Android 中创建自定义评分栏

转载 作者:IT老高 更新时间:2023-10-28 13:13:48 24 4
gpt4 key购买 nike

您好,我需要在我的应用程序中执行评分...所以我需要创建自定义评分栏...有人可以帮助我吗?

最佳答案

编辑

查看摩托罗拉的自定义评级http://community.developer.motorola.com/t5/Android-App-Development-for/custom-rating-bar-style-using-android-s-ratingBar-small-style/td-p/10462

更新

styles.xml

这必须位于您的值文件夹中

 <?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="foodRatingBar" parent="@android:style/Widget.RatingBar">
<item name="android:progressDrawable">@drawable/food_rating_bar_full</item>
<item name="android:minHeight">23dip</item>
<item name="android:maxHeight">25dip</item>
</style>
</resources>

food_rating_bar_full.xml

此文件必须在 Drawable 文件夹中。

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

food_ratingbar_full_empty.xml

此文件必须在 Drawable 文件夹中。

<?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/cookiee" />

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

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

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

</selector>

food_ratingbar_full_filled.xml

此文件必须位于 Drawable 文件夹中。

<?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_pressed="true"
android:state_window_focused="true"
android:drawable="@drawable/cookie" />

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

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

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

</selector>

main.xml 文件应如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<RatingBar android:id="@+id/ratingBar1"
style="@style/foodRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

</RatingBar>
</LinearLayout>

MainActivity.class 应该如下所示:

import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity {
/** Called when the activity is first created. */

RatingBar rb;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

rb=(RatingBar)findViewById(R.id.ratingBar1);

rb.setOnRatingBarChangeListener(new OnRatingBarChangeListener(){

@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),Float.toString(rating),Toast.LENGTH_LONG).show();

}

});
}
}

我使用了两张图片:

cookie.jpg

cookiee.jpg

这两张图片大小相同,一张用于识别选中的Rating Bar,另一张用于识别未选中的RatingBar

关于android - 如何在 Android 中创建自定义评分栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5800657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com