Android:align_left与中心对齐 [英] Android: align_left aligns to the center

查看:157
本文介绍了Android:align_left与中心对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a fragment that contains a google map:

<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" />

地图占用整个屏幕并启用了mylocation:

the map takes up the entire screen and has mylocation enabled:

 map.setMyLocationEnabled(true);

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

The following code should align the mylocation button to the top-left of the screen:

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

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

however - this only aligns the button to the top center of the screen. However, if I add this:

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

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

then it aligns to the left properly. The problem is that ALIGN_PARENT_START can't be used programmatically before api 17 and I need to support from a lower api level. Why isn't align_parent_left working on it's own to align the button to the left?

推荐答案

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

Here's my implementation of moving the my location button:

<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>
      .
      .
      .

在我的活动中:

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


        }
    });

这篇关于Android:align_left与中心对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆