安卓:align_left对齐到中心

5
我有一个包含Google地图的片段:

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

<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.setMyLocationEnabled(true);

以下代码应该将“我的位置”按钮对齐到屏幕的左上角:
 // 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 单独使用不能将按钮向左对齐?


1
尝试将您的MapView的父视图设置为RelativeLayout或FrameLayout。 - MetaSnarf
1
请查看此链接:https://dev59.com/d4jca4cB1Zd3GeqPsSf1#31337222 - MetaSnarf
@MetaSnarf - 这是一个相对布局 - 否则它根本无法工作(即使使用align_parent_start)。关于您提供的链接->这是我已经在使用的移动mylocation按钮的方法,但它并没有解释为什么align_parent_left会将按钮居中,而align_parent_start会将其对齐到屏幕左侧。 - Jon
1
关于使用框架布局怎么样? - MetaSnarf
@MetaSnarf - 我尝试过这个方法,包括将xml的rootlayout替换为framelayout,但无论我怎么做,我总是收到一个错误,指出framelayout无法转换为relativelayout。看起来它在某种方式上被定义为relativelayout。 - Jon
1
我已经发布了一个答案。那是我的实现,它运行得非常好。 - MetaSnarf
2个回答

8
这是我移动“我的位置”按钮的实现方式:
<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);


        }
    });

我一开始对此很有希望,当我将代码更改为相同的后编译,并看到我的位置按钮出现在右下角,即使我无法从代码中理解是什么导致它去到右边。 但是,我尝试修改您的代码以使按钮出现在左侧的所有尝试都没有成功。 我只能使用此代码将其从右上角移动到右下角-这使我非常遗憾,因为仍然存在相同的问题->无法在API 16中显示按钮。 无论如何,非常感谢您的帮助。 - Jon
你可以尝试创建自己的位置按钮并将其添加到框架布局中。这样,您就不会遇到在屏幕上移动它时出现的问题。 - MetaSnarf

0

不幸的是,其中一种方法是从头开始创建它。

        RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams((int)(45*density),(int)(45*density));
        rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        rlp.setMargins((int)(5*density), 0, 0, (int)(5*density));
        locationButton.setLayoutParams(rlp);

其中密度是,例如,

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    dispWidth = metrics.widthPixels;
    dispHeight = metrics.heightPixels;
    density = metrics.scaledDensity;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接