即使使用WRAP_CONTENT,setNumstars()也无法正常工作

时间:2017-03-19 14:09:13

标签: android ratingbar

在地图中,当我触摸标记时,会出现一个警告框,显示用户的反馈评级。 我使用了setNumstars(5)并使用了WRAP_CONTENT的布局,但由于有大量的星星,我没有得到所需的输出。

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
List<String> location = new ArrayList<>();
double lat, lng;
String pname,address,timing,tin;
Double deflat=13.0826802, deflng=80.2707184;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    location.addAll(patient_final.latlong);}    @Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    for (String str : location) {
        String temp[] = str.split(",");
        lat = Double.parseDouble(temp[0]);
        lng = Double.parseDouble(temp[1]);
        pname = temp[2];
        address=temp[3];
        tin=temp[4];
        timing=temp[5];
        mMap.addMarker(new MarkerOptions().position(new LatLng(lat, lng)).title(pname+"-"+tin).snippet(address+" Timing: "+timing));
    }
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(deflat, deflng), 10.5f));
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

        mMap.setMyLocationEnabled(true);
    }

    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
            String snip[]=marker.getTitle().split("-");
            AlertDialog.Builder builder = new AlertDialog.Builder(MapsActivity.this);
             builder.setTitle("Feedback for "+snip[0]);
            builder.setMessage("Rate the shop based on your previous experience below ");

            RatingBar rating = new RatingBar(MapsActivity.this);
            rating.setNumStars(5);   //here i used only 5
            rating.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT));
            builder.setView(rating);
            builder.setCancelable(false);
            builder.setPositiveButton("OK", null);
            builder.show();
            return false;
        }
    });


}

}

SCreenShot

1 个答案:

答案 0 :(得分:1)

在其他视图组父级中添加RatingBar,如下面给出的......它将起作用...

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
            AlertDialog.Builder builder = new AlertDialog.Builder(Main2Activity.this);
            builder.setTitle("Feedback for ");
            builder.setMessage("Rate the shop based on your previous experience below ");

            LinearLayout ll = new LinearLayout(Main2Activity.this);

            RatingBar rating = new RatingBar(Main2Activity.this);
            rating.setNumStars(5);   //here i used only 5
            rating.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT));
            ll.addView(rating);
            builder.setView(ll);
            builder.setCancelable(false);
            builder.setPositiveButton("OK", null);

            builder.show();
            return false;
        }
    });

这里我在LinearLayout中添加了评级栏。

希望这会对你有所帮助。