【问题标题】:Height MapFragment Layout not work高度 MapFragment 布局不起作用
【发布时间】:2014-01-17 07:43:13
【问题描述】:

我正在构建一个包含两个选项卡主机(顶部和底部)的应用程序,并且在一个选项卡中,我想通过 Google API 放置一个 Google 地图。一切正常,但地图显示不正确。您可以在这张图片中看得更清楚:

我的代码是:

fragment_mapa.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

</RelativeLayout>

MapaFragment.java

public class MapaFragment extends Fragment {
    public MapaFragment(){}

    public GoogleMap mapa;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_mapa, container, false);

        if (container != null) {
            container.removeAllViews();
        }

        GoogleMap map = ((SupportMapFragment) getActivity().getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();

        ((MainActivity) getActivity()).setBar();

        //Asigno el nombre de la vista e integro tabhost-sliding
        ((MainActivity) getActivity()).setTitle("Mapa");
        ((MainActivity) getActivity()).integrationMenu();

        return rootView;

    }


    public void onDestroyView() {
        super.onDestroyView();
        Log.d("DESTROY", "onDestroyView");

        Fragment f = getActivity()
                .getSupportFragmentManager().findFragmentById(R.id.map);

        if (f != null) {
            getActivity().getSupportFragmentManager()
            .beginTransaction().remove(f).commit();
        }
    }
}

还有我的activity_main.xml,其中包含两个tabhost和一个调用片段的framelayout:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Estructura de los dos tabhost -->
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/LinearLayout1"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- TABHOST SUPERIOR -->
        <RelativeLayout
            android:id="@+id/l1"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:gravity="right"
            android:layout_above="@+id/l2"
            android:layout_alignParentTop="true">

            <android.support.v4.app.FragmentTabHost
                android:id="@+id/tabhost_sup"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TabWidget
                        android:id="@android:id/tabs"
                        android:orientation="horizontal"
                        android:layout_width="fill_parent"
                        android:layout_height="50dip"
                        android:layout_weight="0"/>


                    <ScrollView 
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="#ffffff">
                        <FrameLayout
                            android:id="@android:id/tabcontent"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_weight="0"/>
                    </ScrollView>

                </LinearLayout>
            </android.support.v4.app.FragmentTabHost>
        </RelativeLayout>

        <!-- TABHOST INFERIOR -->
        <RelativeLayout
            android:id="@+id/l2"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="50dip"
            android:layout_weight="1"
            android:gravity="right"
            android:layout_alignParentBottom="true"
            android:background="@android:color/background_light" >

            <android.support.v4.app.FragmentTabHost
                android:id="@android:id/tabhost"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <LinearLayout
                    android:id="@+id/RelativeLayout1"
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">

                    <FrameLayout
                        android:id="@android:id/tabcontent"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:layout_weight="0"/>

                    <TabWidget
                        android:id="@android:id/tabs"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="0" 
                        android:layout_marginBottom="0dp"/>

                </LinearLayout>
            </android.support.v4.app.FragmentTabHost>
        </RelativeLayout>
    </RelativeLayout>

    <!-- Listview to display slider menu -->
    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"       
        android:listSelector="@drawable/list_selector"
        android:background="@color/list_background"/>

    <ListView 
        android:id="@+id/right_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"       
        android:listSelector="@drawable/list_selector"
        android:background="@color/list_background"/>

</android.support.v4.widget.DrawerLayout>

【问题讨论】:

  • 尝试删除滚动视图!!!
  • 很好!!现在它起作用了。非常感谢。

标签: java android layout fragment


【解决方案1】:

解决方案(Hardik):

删除我的activity_main.xml中的滚动视图,它运行良好。

【讨论】:

    【解决方案2】:

    删除滚动视图不是很方便的解决方案。代替它尝试 android:FillViewPort="True" 代替 Scrollview 在您的 layout 解决您可能需要滚动活动内容的所有屏幕的问题。希望这对您有所帮助!!

    【讨论】:

    • 不行不行
    【解决方案3】:

    使用 android 工具的 hierarchiviewer 检查布局值。

    【讨论】:

      【解决方案4】:

      对于未来: 如果你有一个 ScrollView,那么元素必须指定它们的高度(水平滚动视图的宽度),否则它们应该有什么高度,无限?

      我还遇到了一个问题,即在将地图片段放置在高度 = Wrap_Content 且高度最小的相对布局中时发生的确切情况。
      设置一个恒定的高度解决了这个问题,同样的想法。

      【讨论】:

        【解决方案5】:

        遇到了同样的问题,但我的地图片段在滚动视图内,它需要有滚动视图,因为除了地图之外还有其他视图。如果它只包含地图,则不需要滚动视图。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-11
          • 2013-10-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-11
          相关资源
          最近更新 更多