MapFragment Styled as Dialog causes TextView to be transparent

可紊 提交于 2019-12-11 00:55:35

问题


Here's the setup:

I'm constructing and displaying Activities that are styled as centered Dialogs. This is to show hierarchical content that shouldn't be full-screen on the device.

One type of content is a map. So i've successfully loaded a MapFragment into a Dialog-styled FragmentActivity. This works really well.

The problem is when I attempt to place a new TextView (acting as a custom title bar) above the map. The issue is that the TextView's content seems to become transparent and the previous Activity's content bleeds through.

The exact same "design" works just fine when the map's activity allows the map to be full screen. The problem is only when i try to make the map a custom size (and also be displayed along with another view).

Here's the Activity:

public class MapViewActivity extends FragmentActivity {

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

            requestWindowFeature(Window.FEATURE_NO_TITLE);

            setContentView(R.layout.activity_map_view);

            Window window = this.getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
            window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

            WindowManager.LayoutParams wmlp = window.getAttributes();
            wmlp.y -= 10;
            window.setAttributes(wmlp);

            Intent i = getIntent();
            String lat = i.getExtras().getString("lat");
            String lng = i.getExtras().getString("lng");
            String title = i.getExtras().getString("title");
            String snippet = i.getExtras().getString("address");

            TextView tv = (TextView)this.findViewById(R.id.mapTitle);
            tv.setText(title);

            SupportMapFragment mf = (SupportMapFragment)this.getSupportFragmentManager().findFragmentById(R.id.map);

            MarkerOptions mo = new MarkerOptions();
            mo.position(new LatLng( Double.valueOf(lat), Double.valueOf(lng) ));
            mo.title(title);
            mo.snippet(snippet);

            GoogleMap gm = mf.getMap();

            gm.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng( Double.valueOf(lat), Double.valueOf(lng) ), 14));
            gm.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            gm.setMyLocationEnabled(true);
            gm.addMarker(mo);

    }
}

Here's the layout xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="300dp"
    android:layout_height="365dp" 
    tools:context=".MapViewActivity" >

    <TextView 
        android:id="@+id/mapTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:textSize="18sp"
        android:textColor="@color/white"
        android:background="@color/blue"
        android:text="TEST TITLE LABEL"
    >
    </TextView>

    <fragment
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="280dp"
      android:layout_below="@id/mapTitle"
      class="com.google.android.gms.maps.SupportMapFragment" />

</RelativeLayout>

Here's the style that I use when defining the Activity in the manifest:

    <style name="SubViewTheme" parent="android:Theme.Dialog">
    <item name="android:windowAnimationStyle">@style/MainViewAnimation</item>
        <item name="android:windowBackground">@color/white</item>
        <item name="android:background">@null</item>
    </style>

All i'm doing to show the activity is using a standard start startActivity(i);

And here's a screen cap of the outcome. Sorry for the poor quality. It's a pain to get a screen cap of my Nexus1.

"screen shot of the map dialog"

In the screen shot you'll see the "dialog" activity with the map. And the TextView IS appearing - the TextView is the small dark-blue rectangle at the top right of the map. then to the left of the messed up label we have the text from the previous dialog bleeding through where the TextView SHOULD be.

Is there anything obvious that I'm doing wrong? Maybe i'm being too ambitious with the new MapFragment support :).

This project requires support back to 2.3.3 so i'm stuck with the support libs.

Thanks for any help. Let me know if anything in my description doesn't make sense.


回答1:


Oh god I can't believe this is the only answer that I have found....

Create another fragment and on create do a fragmentTransaction to hide your map fragment let this run for a period of time (I waited for my Google Directions calls/functions to return) then do another FT to show the mapFragment this fixed the transparency issue.

in onCreate

final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.hide(mDirMapFrag);
ft.commit();
getSupportFragmentManager().executePendingTransactions();

//Hides the map, list fragment is shown my default.

//in my method which is returned from the google directions methods but you could just do a quick Handler h = new Handler(); h.postDelayed(new Runnable() {} etc.

final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.show(mDirMapFrag);
ft.commit();
getSupportFragmentManager().executePendingTransactions();

Does anyone have a better solution than this?




回答2:


Put a transparent View on top of the map.

<RelativeLayout
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true" />

<View
    android:id="@+id/MyView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent" />


来源:https://stackoverflow.com/questions/14126594/mapfragment-styled-as-dialog-causes-textview-to-be-transparent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!