2

我想将地图视图设置为全屏,我已经完成了所有必要的事情,例如将框架布局和地图视图都设置为 matchparent 但是当我在手机上运行应用程序时仍然只有一部分(可能 60dp 是高度但宽度是全屏) 的地图可见。

这是我的一些布局 xml 文件。

//// 对于片段映射 ///////////

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapFragment">

    <fragment
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:id="@+id/frg"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

//// 对于片段映射 ///////////

//// 用于 java 片段映射代码 //////

public class MapFragment extends Fragment {

    public MapFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View rootView = inflater.inflate(R.layout.fragment_map, container, false);

        SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.frg);  //use SuppoprtMapFragment for using in fragment instead of activity  MapFragment = activity   SupportMapFragment = fragment
        mapFragment.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap mMap) {
                LatLng customMarkerLocationOne = new LatLng(15.492554, 120.972169);
                LatLng customMarkerLocationTwo = new LatLng(15.483516, 120.962996);
                LatLng customMarkerLocationThree = new LatLng(15.486315, 120.973318);
                LatLng customMarkerLocationFour = new LatLng(15.485838, 120.972290);
                mMap.addMarker(new MarkerOptions().position(customMarkerLocationOne).
                        icon(BitmapDescriptorFactory.fromBitmap(
                                createCustomMarker(rootView.getContext(),R.drawable.yumin,"Yumin")))).setTitle("Korean Food House");
                mMap.addMarker(new MarkerOptions().position(customMarkerLocationTwo).
                        icon(BitmapDescriptorFactory.fromBitmap(
                                createCustomMarker(rootView.getContext(),R.drawable.puregold,"Puregold")))).setTitle("Grocery Store");

                mMap.addMarker(new MarkerOptions().position(customMarkerLocationThree).
                        icon(BitmapDescriptorFactory.fromBitmap(
                                createCustomMarker(rootView.getContext(),R.drawable.mrsg,"Mrs. G")))).setTitle("Cake Shop");
                mMap.addMarker(new MarkerOptions().position(customMarkerLocationFour).
                        icon(BitmapDescriptorFactory.fromBitmap(
                                createCustomMarker(rootView.getContext(),R.drawable.actron,"Actron")))).setTitle("Electronic Shop");

                //LatLngBound will cover all your marker on Google Maps
                LatLngBounds.Builder builder = new LatLngBounds.Builder();
                builder.include(customMarkerLocationOne); //Taking Point A (First LatLng)
                builder.include(customMarkerLocationThree); //Taking Point B (Second LatLng)
                LatLngBounds bounds = builder.build();
                CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 200);
                mMap.moveCamera(cu);
                mMap.animateCamera(CameraUpdateFactory.zoomTo(14), 2000, null);
            }
        });

        return rootView;
    }

    public static Bitmap createCustomMarker(Context context, @DrawableRes int resource, String _name) {

        View marker = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.bitmap_google_marker_layout_item, null);

        CircleImageView markerImage = (CircleImageView) marker.findViewById(R.id.user_dp);
        markerImage.setImageResource(resource);
        TextView txt_name = (TextView)marker.findViewById(R.id.name);
        txt_name.setText(_name);

        DisplayMetrics displayMetrics = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        marker.setLayoutParams(new ViewGroup.LayoutParams(52, ViewGroup.LayoutParams.WRAP_CONTENT));
        marker.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
        marker.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
        marker.buildDrawingCache();
        Bitmap bitmap = Bitmap.createBitmap(marker.getMeasuredWidth(), marker.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        marker.draw(canvas);

        return bitmap;
    }
}

//// 用于 java 片段映射代码 ///////////

//// 用于 Home Activity ///////////

<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/container"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeBuyersActivity">


    <EditText
        android:id="@+id/search_bar_home"
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:layout_marginStart="32dp"
        android:layout_marginEnd="8dp"
        android:background="@drawable/search_card"
        android:elevation="3dp"
        android:ems="10"
        android:textSize="12sp"
        android:inputType="textPersonName"
        android:padding="4dp"
        android:textColor="#333333"
        app:layout_constraintBottom_toBottomOf="@+id/search_icon_home"
        app:layout_constraintEnd_toStartOf="@+id/search_icon_home"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/search_icon_home" />

    <ImageView
        android:id="@+id/search_icon_home"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginEnd="50dp"
        android:background="@drawable/searchhh"
        android:elevation="3dp"
        android:tint="#333333"
        app:layout_constraintBottom_toBottomOf="@+id/cart_icon_home"
        app:layout_constraintEnd_toStartOf="@+id/cart_icon_home"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <ImageView
        android:id="@+id/cart_icon_home"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:background="@drawable/icon_for_cart"
        android:elevation="3dp"
        android:textAlignment="textEnd"
        android:tint="#333333"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <ScrollView
        android:id="@+id/fragment_container"
        android:layout_width="0dp"
        android:layout_height="0px"
        app:layout_constraintBottom_toTopOf="@+id/nav_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="@drawable/card_white_sharp_edges"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

</androidx.constraintlayout.widget.ConstraintLayout>

//// 用于 Home Activity ///////////

//// 对于 Home Activity Java ///////////

public class HomeBuyersActivity extends AppCompatActivity {

    private FirebaseAuth mAuth;
    private CallbackManager callbackManager;
    private String TAG = "";

    private EditText search_bar_home;
    private ImageView search_icon_home, cart_icon_home;
    private Toolbar toolbar;
    private boolean mLocationPermissionGranted = false;


    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            mAuth = FirebaseAuth.getInstance();
            callbackManager = CallbackManager.Factory.create();

            search_bar_home = findViewById(R.id.search_bar_home);
            search_icon_home = findViewById(R.id.search_icon_home);
            cart_icon_home = findViewById(R.id.cart_icon_home);

            toolbar = findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            if (getSupportActionBar() != null) {
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            }

            final Intent intent = getIntent();
            final String UserID = intent.getStringExtra("userid");
            final String Firstname = intent.getStringExtra("firstname");
            final String Lastname = intent.getStringExtra("lastname");
            final String Email = intent.getStringExtra("email");
            final String Image = intent.getStringExtra("imageurl");
            final String ReferralCOde = intent.getStringExtra("refer");

            Paper.init(HomeBuyersActivity.this);
            final String UserEmailKey = Paper.book().read(Prevalent.UserEmailKey);
            final String UserPasswordKey = Paper.book().read(Prevalent.UserPasswordKey);

            switch (item.getItemId()) {
                case R.id.navigation_home:
                    HomeFragment fragment = new HomeFragment();
                    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.fragment_container, fragment);
                    fragmentTransaction.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
                    fragmentTransaction.commit();
                    search_bar_home.setVisibility(View.VISIBLE);
                    search_icon_home.setVisibility(View.VISIBLE);
                    cart_icon_home.setVisibility(View.VISIBLE);
                    return true;
                case R.id.navigation_map:
                    MapFragment map = new MapFragment();
                    Bundle bundlem = new Bundle();
                    bundlem.putString("firstname", Firstname);
                    bundlem.putString("lastname", Lastname);
                    bundlem.putString("imageurl", Image);
                    bundlem.putString("userid", UserID);
                    bundlem.putString("email", Email);
                    bundlem.putString("refer", ReferralCOde);

                    map.setArguments(bundlem);
                    FragmentTransaction fragmentTransactionm = getSupportFragmentManager().beginTransaction();
                    fragmentTransactionm.replace(R.id.fragment_container, map);
                    fragmentTransactionm.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
                    fragmentTransactionm.commit();
                    search_bar_home.setVisibility(View.INVISIBLE);
                    search_icon_home.setVisibility(View.INVISIBLE);
                    cart_icon_home.setVisibility(View.INVISIBLE);
                    return true;
                case R.id.navigation_notifications:
                    search_bar_home.setVisibility(View.INVISIBLE);
                    search_icon_home.setVisibility(View.INVISIBLE);
                    cart_icon_home.setVisibility(View.INVISIBLE);
                    NotificationFragment nf = new NotificationFragment();
                    Bundle bundlen = new Bundle();
                    bundlen.putString("firstname", Firstname);
                    bundlen.putString("lastname", Lastname);
                    bundlen.putString("imageurl", Image);
                    bundlen.putString("userid", UserID);
                    bundlen.putString("email", Email);
                    bundlen.putString("refer", ReferralCOde);

                    nf.setArguments(bundlen);
                    FragmentTransaction fragmentTransactionn = getSupportFragmentManager().beginTransaction();
                    fragmentTransactionn.replace(R.id.fragment_container, nf);
                    fragmentTransactionn.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
                    fragmentTransactionn.commit();
                    return true;
                case R.id.navigation_profile:
                    ProfileFragment fragment1 = new ProfileFragment();
                    Bundle bundle = new Bundle();
                    bundle.putString("firstname", Firstname);
                    bundle.putString("lastname", Lastname);
                    bundle.putString("imageurl", Image);
                    bundle.putString("userid", UserID);
                    bundle.putString("email", Email);
                    bundle.putString("refer", ReferralCOde);

                    fragment1.setArguments(bundle);
                    FragmentTransaction fragmentTransaction1 = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction1.replace(R.id.fragment_container, fragment1);
                    fragmentTransaction1.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
                    fragmentTransaction1.commit();
                    search_bar_home.setVisibility(View.INVISIBLE);
                    search_icon_home.setVisibility(View.INVISIBLE);
                    cart_icon_home.setVisibility(View.INVISIBLE);
                    return true;
            }
            return false;
        }
    };

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

        BottomNavigationView navView = findViewById(R.id.nav_view);
        navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        cart_icon_home = findViewById(R.id.cart_icon_home);
        search_bar_home = findViewById(R.id.search_bar_home);
        search_icon_home = findViewById(R.id.search_icon_home);

        final HomeFragment fragment = new HomeFragment();
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();

        cart_icon_home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(HomeBuyersActivity.this, MyCartFragment.class);
                startActivity(intent);
            }
        });
    }
}
4

1 回答 1

0

对“fragment_container”使用 FrameLayout 而不是 Scrollview

于 2019-09-01T11:26:27.467 回答