gpt4 book ai didi

java - Android: align_left 居中对齐

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:36:30 27 4
gpt4 key购买 nike

我有一个包含谷歌地图的 fragment :

<com.google.android.gms.maps.MapView
android:id="@+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraZoom="13"
map:mapType="normal"
map:uiZoomControls="false"
map:uiRotateGestures="true"
map:uiScrollGestures="true"
map:uiZoomGestures="true"
map:uiTiltGestures="false" />

map 占据了整个屏幕并启用了我的位置:

 map.setMyLocationEnabled(true);

以下代码应该将 mylocation 按钮对齐到屏幕的左上角:

 // Get the mylocation button view
View locationButton = ((View) mapview.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));

RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();

rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

但是 - 这只会将按钮对齐到屏幕的顶部中心。但是,如果我添加这个:

if (Build.VERSION.SDK_INT >= 17) {
rlp.addRule(RelativeLayout.ALIGN_PARENT_START);
}

然后它正确地向左对齐。问题是 ALIGN_PARENT_START 不能在 api 17 之前以编程方式使用,我需要从较低的 api 级别支持。为什么 align_parent_left 不能自行将按钮左对齐?

最佳答案

这是我移动我的位置按钮的实现:

<LinearLayout
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:name="com.google.android.gms.maps.SupportMapFragment">
</fragment>
.
.
.

关于我的 Activity :

mapFragment = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.myMap));

//if you're using a fragment use:
//mapFragment = ((SupportMapFragment) getChildFragmentManager() .findFragmentById(R.id.myMap));


mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
//add map adjustments here//


//then this is where you move the my location button

View locationButton = ((View) mapFragment.getView().findViewById(Integer.parseInt("1")).
getParent()).findViewById(Integer.parseInt("2"));

// and next place it, for exemple, on bottom right (as Google Maps app)
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
// position on right bottom
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
rlp.setMargins(0, 0, 30, 30);


}
});

关于java - Android: align_left 居中对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31591524/

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