Android MapView 未在 CardView 中填充父项
Android MapView not filling parent inside CardView
让我简要解释一下我正在尝试做什么。我正在从 Yelp 获取一家餐厅,并将餐厅的属性放入 CardView。我想要 CardView 的原因是因为我想要滑动功能。我希望能够向左、向右和向上滑动。 To do this, I need a RecyclerView to handle every direction (or so I've come to understand from reading this).如果有人有更好的方法请告诉我。
出于某种原因,当您将 MapView 的 layout_height
设置为具体值时,它会很好地调整大小。但它不会填充父项。
我不想为明显的兼容性问题设置具体值。
此外,是否可以在 CardView 的 MapView 部分禁用滑动?
这是我的布局文件:
fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:showIn="@layout/activity_main"
tools:context=".MainActivityFragment">
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:id="@+id/radioGroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Use Zip Code"
android:id="@+id/useZip"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Use Location"
android:id="@+id/useLocation"
android:checked="false" />
</RadioGroup>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/zipcode"
android:hint="zip code"
android:layout_below="@+id/radioGroup"
android:layout_centerHorizontal="true"
android:visibility="gone" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Generate"
android:id="@+id/generate"
android:layout_gravity="bottom|center_horizontal"
android:layout_below="@+id/zipcode"
android:layout_centerHorizontal="true" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_below="@+id/generate"
android:layout_alignParentBottom="true">
<android.support.v7.widget.RecyclerView
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/container">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</RelativeLayout>
restaurant_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/restaurantContainer"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/tools">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="@dimen/_75sdp"
android:layout_height="@dimen/_75sdp"
android:id="@+id/thumbnail"
android:layout_margin="@dimen/_5sdp"/>
<ImageView
android:layout_width="@dimen/_75sdp"
android:layout_height="@dimen/_15sdp"
android:id="@+id/rating"
android:layout_margin="@dimen/_5sdp"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/name"
android:gravity="top"
android:layout_margin="@dimen/_5sdp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/categories"
android:layout_margin="@dimen/_5sdp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.gms.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mapView"
map:mapType="normal"
map:liteMode="true"
map:cameraZoom="14"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
问题截图如下:
要么将 CardView
高度设置为 200dp
,将 MapView 设置为 fill_parent。
这样会填满剩余高度,你可以调整CardView
高度。或将 CardView
高度设置为 wrap_content,然后提供您的 MapView
高度,例如 200dp
(或您想要的任何高度)。
更新:RecyclerView 是一个滚动视图。这意味着它有无限的高度,你放入其中的每张卡片都应该有自己的高度,卡片不能有 fill_parent
.
现在,如果您希望 MapView 具有剩余的高度。我假设你的意思是卡片高度的其余部分。首先,您必须定义您希望卡的长度。
例如:如果您将 Card 高度设置为 500dp,将 MapView 设置为 fill_parent,并且 Card 中除 MapView 之外的其他内容为 100dp,则 MapView 将占用剩余的高度 (400dp)。
如果您希望 MapView 占据移动屏幕上的剩余高度,那么您必须删除 RecyclerView 并只使用 Card。
让我简要解释一下我正在尝试做什么。我正在从 Yelp 获取一家餐厅,并将餐厅的属性放入 CardView。我想要 CardView 的原因是因为我想要滑动功能。我希望能够向左、向右和向上滑动。 To do this, I need a RecyclerView to handle every direction (or so I've come to understand from reading this).如果有人有更好的方法请告诉我。
出于某种原因,当您将 MapView 的 layout_height
设置为具体值时,它会很好地调整大小。但它不会填充父项。
我不想为明显的兼容性问题设置具体值。
此外,是否可以在 CardView 的 MapView 部分禁用滑动?
这是我的布局文件:
fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:showIn="@layout/activity_main"
tools:context=".MainActivityFragment">
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:id="@+id/radioGroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Use Zip Code"
android:id="@+id/useZip"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Use Location"
android:id="@+id/useLocation"
android:checked="false" />
</RadioGroup>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/zipcode"
android:hint="zip code"
android:layout_below="@+id/radioGroup"
android:layout_centerHorizontal="true"
android:visibility="gone" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Generate"
android:id="@+id/generate"
android:layout_gravity="bottom|center_horizontal"
android:layout_below="@+id/zipcode"
android:layout_centerHorizontal="true" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_below="@+id/generate"
android:layout_alignParentBottom="true">
<android.support.v7.widget.RecyclerView
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/container">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</RelativeLayout>
restaurant_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/restaurantContainer"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/tools">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="@dimen/_75sdp"
android:layout_height="@dimen/_75sdp"
android:id="@+id/thumbnail"
android:layout_margin="@dimen/_5sdp"/>
<ImageView
android:layout_width="@dimen/_75sdp"
android:layout_height="@dimen/_15sdp"
android:id="@+id/rating"
android:layout_margin="@dimen/_5sdp"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/name"
android:gravity="top"
android:layout_margin="@dimen/_5sdp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/categories"
android:layout_margin="@dimen/_5sdp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.gms.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mapView"
map:mapType="normal"
map:liteMode="true"
map:cameraZoom="14"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
问题截图如下:
要么将 CardView
高度设置为 200dp
,将 MapView 设置为 fill_parent。
这样会填满剩余高度,你可以调整CardView
高度。或将 CardView
高度设置为 wrap_content,然后提供您的 MapView
高度,例如 200dp
(或您想要的任何高度)。
更新:RecyclerView 是一个滚动视图。这意味着它有无限的高度,你放入其中的每张卡片都应该有自己的高度,卡片不能有 fill_parent
.
现在,如果您希望 MapView 具有剩余的高度。我假设你的意思是卡片高度的其余部分。首先,您必须定义您希望卡的长度。
例如:如果您将 Card 高度设置为 500dp,将 MapView 设置为 fill_parent,并且 Card 中除 MapView 之外的其他内容为 100dp,则 MapView 将占用剩余的高度 (400dp)。
如果您希望 MapView 占据移动屏幕上的剩余高度,那么您必须删除 RecyclerView 并只使用 Card。