Tôi biết nó đã được trả lời và chấp nhận nhưng những câu trả lời này không hiệu quả với tôi. Có lẽ đã có những thay đổi trong RelativeLayout.LayoutParams hoặc thậm chí trong GoogleMaps kể từ khi nó được trả lời.
Trong nỗ lực của tôi với các câu trả lời hiện có, tôi nhận ra rằng có thể có nhiều quy tắc LayoutParam cho nút la bàn ghi đè nỗ lực di chuyển nút la bàn của tôi. Tôi đã thử nghiệm bằng cách xóa một số thông số như sau:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)
locationButton.getLayoutParams();
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_START);
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_END);
Và sau đó thêm quy tắc mới như đã thực hiện trong một số câu trả lời. Nhưng nó vẫn không hoạt động như mong đợi. Thường xuyên hơn không, nút ở đâu đó ở phía bên trái.
Tôi quyết định chỉ tạo LayoutParams mới và thay thế các bảng hiện có và ... nó hoạt động hoàn hảo!
Tôi ban đầu đã sử dụng thẻ xem cho GoogleMapCompass
, nhưng câu hỏi liên quan đến GoogleMapMyLocationButton
. Vì vậy, tôi đã nhận xét ra dòng GoogleMapCompass
và đặt trong dòng GoogleMapMyLocationButton
.
Đây là mã cho MapFragment.java:
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
public class MapFragment extends Fragment implements OnMapReadyCallback {
private static final String TAG = MapFragment.class.getSimpleName();
private SupportMapFragment mapFragment = null;
public MapFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_map, container, false);
Log.d(TAG, "onCreateView()");
mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.setRetainInstance(true);
mapFragment.getMapAsync(this);
return view.findViewById(R.id.map);
}
@Override
public void onMapReady(GoogleMap googleMap) {
Log.d(TAG, "onMapReady()");
View mapView = mapFragment.getView();
moveCompassButton(mapView);
}
/**
* Move the compass button to the right side, centered vertically.
*/
public void moveCompassButton(View mapView) {
try {
assert mapView != null; // skip this if the mapView has not been set yet
Log.d(TAG, "moveCompassButton()");
// View view = mapView.findViewWithTag("GoogleMapCompass");
View view = mapView.findViewWithTag("GoogleMapMyLocationButton");
// move the compass button to the right side, centered
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
layoutParams.setMarginEnd(18);
view.setLayoutParams(layoutParams);
} catch (Exception ex) {
Log.e(TAG, "moveCompassButton() - failed: " + ex.getLocalizedMessage());
ex.printStackTrace();
}
}
}
Tôi không nghĩ là có thể. –
định dạng mã tốt hơn và sửa một số định dạng ngữ pháp – Robert