SupportMapFragment,顶部有自定义视图

时间:2016-05-24 13:29:52

标签: android google-maps fragment android-custom-view android-gravity

我在自定义SupportMapFragment布局时遇到了一些问题。我基本上想在地图片段布局的底部添加一个自定义视图。

我设法以编程方式将视图添加到片段中:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View mapView = super.onCreateView(inflater, container, savedInstanceState);
    View customView = inflater.inflate(R.layout.custom_view, container, false);

    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) customView.getLayoutParams();
    params.gravity = Gravity.BOTTOM;
    customView.setLayoutParams(params);

    FrameLayout wrapper = new FrameLayout(inflater.getContext());
    wrapper.addView(mapView);
    wrapper.addView(customView);

    return wrapper;
}

custom_view 布局是一个简单的CardView,没有复杂的逻辑:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/custom_view_root"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:visibility="gone"
        app:cardBackgroundColor="@color/white"
        >

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >

            ... some basic content

    </LinearLayout>


</android.support.v7.widget.CardView>

问题是我的自定义视图未显示在底部,而是显示在顶部。似乎我没有考虑到我设定的重力特性。 我还尝试使用RelativeLayout作为包装器并手动添加规则以对齐底部的自定义视图,但结果是相同的。

LE 正如@Budius在回答中提到的,我忽略了 inflate()方法的 ViewGorup root 参数。

这是使用布局中设置的参数的正确实现:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    FrameLayout wrapper = new FrameLayout(inflater.getContext());

    View mapView = super.onCreateView(inflater, container, savedInstanceState);
    wrapper.addView(mapView);

    customView = inflater.inflate(R.layout.custom_view, wrapper, false);
    wrapper.addView(customView);

    return wrapper;
}

另一个解决方案是以编程方式创建自定义视图的参数,如下所示:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View mapView = super.onCreateView(inflater, container, savedInstanceState);
    customView = inflater.inflate(R.layout.custom_view, container, false);
    FrameLayout wrapper = new FrameLayout(inflater.getContext());
    wrapper.addView(mapView);

    FrameLayout.LayoutParams params =
            new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,
                                         Gravity.BOTTOM);
    wrapper.addView(customView, params);

    return wrapper;
}

1 个答案:

答案 0 :(得分:1)

inflater代码行inflater.inflate(R.layout.custom_view, container, false); container它意味着指示要从XML使用到父布局的LayoutParams。但是你实例化了一个LinearLayout.LayoutParams。然后你将它添加到FrameLayout。所以这一切都被丢弃了,因为它不匹配。

有几种简单的方法可以解决它:

您可以使用正确的父布局对CardView进行充气:

// add this line AFTER you created the FrameLayout
View customView = inflater.inflate(R.layout.custom_view, wrapper, false);

只需更改为wrapper即可直接在true内对CardView进行充气。

// this will correctly use the layout params you set on XML and add the inflated views into wrapper
inflater.inflate(R.layout.custom_view, wrapper, true);

您可以将params创建为正确的类型:

// use FrameLayout.LayoutParams when using FrameLayout as the parent.
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) customView.getLayoutParams();
params.gravity = Gravity.BOTTOM;
customView.setLayoutParams(params);