你知道为什么我的图像按钮在Android Studio中没有正确显示吗?

时间:2017-09-24 00:15:20

标签: android image button maps

我想要做的是在地图上显示一个按钮。我们的想法是触摸按钮并显示更多按钮,这些按钮也是图像。 出于某种原因,图像不会以他们应该的方式出现。除此之外,当应用程序初始化时,您可以看到三个按钮。这不应该发生,因为我希望它们在我按下其中一个之后出现

这就是现在正在发生的事情:

https://imgur.com/a/0iHZ0(抱歉质量不好)

代码

    public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener {

        private GoogleMap mMap;
        LocationManager locationManager;
        String provider;
        LatLng myPosition;

        TextView climaText;
        ImageButton reportes;
        ImageButton playas;
        ImageButton res;
        boolean reportesState = false;



        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maps);

            climaText = (TextView) findViewById(R.id.climaText);

            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            provider = locationManager.getBestProvider(new Criteria(), false);
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            locationManager.requestLocationUpdates(provider, 400, 1, this);
            Location locationActual = locationManager.getLastKnownLocation(provider);
            Double lat = locationActual.getLatitude();
            Double lng = locationActual.getLongitude();
            downloadTask getData = new downloadTask();
            getData.execute("//http://samples.openweathermap.org/data/2.5/weather?lat="+String.valueOf(lat)+"&lon="+String.valueOf(lng)+"&appid=4ec901292084195e70a7a39b5259cf17");


           /* locationRequest = new LocationRequest();
            //locationRequest.setInterval(7500);
            //locationRequest.setFastestInterval(5000);
            locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);*/

            //Si no inicializo aca, debo ponerle final dentro del metodo a cada boton
            reportes = (ImageButton) findViewById(R.id.reporte);
            playas = (ImageButton) findViewById(R.id.reportePlayas);
            res = (ImageButton) findViewById(R.id.reporteRes);

            // 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);
        }

    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        mMap.setMyLocationEnabled(true);

        Location locationActual = locationManager.getLastKnownLocation(provider);;
        if(locationActual != null) {
            // Getting latitude of the current location
            double latitude = locationActual.getLatitude();
            // Getting longitude of the current location
            double longitude = locationActual.getLongitude();
            // Creating a LatLng object for the current location
            myPosition = new LatLng(latitude, longitude);
            mMap.moveCamera(CameraUpdateFactory.newLatLng(myPosition));
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(myPosition, 16));
            }
        }


    @Override
    public void onLocationChanged(Location location) {
        Log.i("TESTTAG", "onLocationChanged called");
        LatLng currentPosition = new LatLng(location.getLatitude(),location.getLongitude());
        mMap.moveCamera(CameraUpdateFactory.newLatLng(currentPosition));
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentPosition, 16));
    }



    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }

    @Override
    protected void onResume() {
        super.onResume();

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        locationManager.requestLocationUpdates(provider, 400, 1, this);

    }

    @Override
    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);

    }


    /*
    public void obtenerClima(){

        CityAsyncTask task = new CityAsyncTask();
        task.execute("http://api.openweathermap.org/data/2.5/weather?q=London,uk");

    }*/

    public void startDialogReportePlayas(View view) {
        AlertDialog.Builder mBuilder = new AlertDialog.Builder(MapsActivity.this);
        View mView = getLayoutInflater().inflate(R.layout.dialog_playas,null);
        mBuilder.setView(mView);
        AlertDialog dialog = mBuilder.create();
        dialog.show();

    }

    public void startDialogReporteRes(View view) {
        AlertDialog.Builder mBuilder = new AlertDialog.Builder(MapsActivity.this);
        View mView = getLayoutInflater().inflate(R.layout.dialog_restaurantes,null);
        mBuilder.setView(mView);
        AlertDialog dialog = mBuilder.create();
        dialog.show();

    }


    public void buttonClickAppear(View view){
        if(!reportesState) {
            playas.setVisibility(View.VISIBLE);
            res.setVisibility(View.VISIBLE);
            reportesState = true;
        }else{
            playas.setVisibility(View.GONE);
            res.setVisibility(View.GONE);
            reportesState = false;
        }
    }

}

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:padding="0dp"
    android:paddingBottom="@dimen/fab_margin"
    android:paddingLeft="@dimen/fab_margin"
    android:paddingRight="@dimen/fab_margin"
    android:paddingTop="@dimen/fab_margin">

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentEnd="true"
        tools:context="com.naluapp.naluapp.MapsActivity"
        android:layout_alignParentTop="true"
        android:layout_alignEnd="@+id/reporte" />

    <TextView
        android:id="@+id/climaText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="150dp"
        android:layout_marginTop="20dp"
        android:elevation="0dp"
        android:text="Hola mundo" />

    <ImageButton
        android:id="@+id/reporte"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="5dp"
        android:layout_marginEnd="5dp"
        android:contentDescription="buttonAppear"
        android:onClick="buttonClickAppear"
        app:srcCompat="@drawable/mapslocicon64pxl" />

    <ImageButton
        android:id="@+id/reporteRes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/reportePlayas"
        android:layout_alignStart="@+id/reportePlayas"
        android:layout_marginBottom="10dp"
        android:contentDescription=""
        android:onClick="startDialogReporteRes"
        app:srcCompat="@drawable/mapslocicon64pxl" />

    <ImageButton
        android:id="@+id/reportePlayas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/reporte"
        android:layout_alignStart="@+id/reporte"
        android:layout_marginBottom="10dp"
        android:contentDescription=""
        android:onClick="startDialogReportePlayas"
        app:srcCompat="@drawable/mapslocicon64pxl" />


</RelativeLayout> 

2 个答案:

答案 0 :(得分:0)

在这种情况下你应该使用 bringToFront,当视图上的调用yourView.bringToFront(); 进入顶部(前面)

reportes.bringToFront();

playas.bringToFront();

res.bringToFront();

例如

#include <iostream>

using namespace std;

int a[100];

int main() {
  cout << "Hello World" << endl;
}

希望它能帮到你

答案 1 :(得分:0)

通过将srCompat更改为src来找到解决方案。