【问题标题】:Extra space in layout android布局android中的额外空间
【发布时间】:2016-02-08 22:38:18
【问题描述】:

我正在关注本教程http://www.androidhive.info/2014/06/android-facebook-like-custom-listview-feed-using-volley/。我复制了这个布局:

recycler_items:

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"

        android:layout_marginLeft="@dimen/feed_item_margin"
        android:layout_marginRight="@dimen/feed_item_margin"
        android:layout_marginTop="@dimen/feed_item_margin"
        android:background="@drawable/bg_parent_rounded_corner"
        android:orientation="vertical"
        android:paddingBottom="@dimen/feed_item_padding_top_bottom"
        android:paddingTop="@dimen/feed_item_padding_top_bottom" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingLeft="@dimen/feed_item_padding_left_right"
            android:paddingRight="@dimen/feed_item_padding_left_right" >

            <com.android.volley.toolbox.NetworkImageView
                android:id="@+id/profilePic"
                android:layout_width="@dimen/feed_item_profile_pic"
                android:layout_height="@dimen/feed_item_profile_pic"
                android:scaleType="fitCenter" >
            </com.android.volley.toolbox.NetworkImageView>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingLeft="@dimen/feed_item_profile_info_padd" >

                <TextView
                    android:id="@+id/name"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:textSize="@dimen/feed_item_profile_name"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/timestamp"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@color/timestamp"
                    android:textSize="@dimen/feed_item_timestamp" />
            </LinearLayout>
        </LinearLayout>

        <TextView
            android:id="@+id/txtStatusMsg"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="5dp"
            android:paddingLeft="@dimen/feed_item_status_pad_left_right"
            android:paddingRight="@dimen/feed_item_status_pad_left_right"
            android:paddingTop="@dimen/feed_item_status_pad_top" />

        <TextView
            android:id="@+id/txtUrl"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:linksClickable="true"
            android:paddingBottom="10dp"
            android:paddingLeft="@dimen/feed_item_status_pad_left_right"
            android:paddingRight="@dimen/feed_item_status_pad_left_right"
            android:textColorLink="@color/link" />

        <info.androidhive.listviewfeed.FeedImageView
            android:id="@+id/feedImage1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:scaleType="fitXY"
            android:visibility="visible" />
    </LinearLayout>

</LinearLayout>

出于某种原因,我在右侧有额外的空白,例如屏幕的 1/5。当我在应用程序中预览它时不存在,但在手机上它是。我知道问题出在这种布局上,因为如果我只用图像替换所有内容,它将填满整个宽度。这是一张有问题的图片:http://6pix.net/images/89726012394405680403.jpg。多余的空间从何而来?

这是我的回收站:

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

    <android.support.v7.widget.RecyclerView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/recycler_view"


        />
</LinearLayout>

这就是我为 recyclerItems 充气的方式:

View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_items, null);

【问题讨论】:

  • 如果您使用 FAB 按钮,您必须使用 CoordinatorLayout。要解决此问题,请将您的 LinearLayout 放入 android 设计支持库中的 CoordinatorLayout 中。使用此网址获取更多信息developer.android.com/reference/android/support/design/widget/…
  • 此布局用于recyclerView,它位于一个片段中,该片段位于使用 CoordinatorLayout 的布局中。
  • 检查您声明 FAB 的代码。那里可能有问题。
  • 不是。我什至删除了 fab 按钮,空间仍然存在。我确信空间来自这种布局。正如我所说,如果我从这个布局中删除除了 FeedImageView(或任何其他元素)之外的所有内容,它们将具有全宽。
  • 出了点问题,但我不知道是哪一部分

标签: android android-layout android-studio android-linearlayout


【解决方案1】:

要将 2 个 XML 标记/图像叠加在一起,请使用 RelativeLayout。我不能 100% 确定您的哪些 XML 标记指的是屏幕上的内容,但可以使用相对布局。例如,也许可以试试这个:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="fill_parent"
    android:layout_height=" fill_parent "
    tools:context="name.company.newapp.Recycler.RecyclerView">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

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

            <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    app:popupTheme="@style/AppTheme.PopupOverlay" />
            </android.support.design.widget.AppBarLayout>

            <include layout="@layout/content_recycler_view" />

        </LinearLayout>


        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            android:src="@android:drawable/ic_dialog_email" />

    </android.support.design.widget.CoordinatorLayout>
</RelativeLayout>

【讨论】:

  • 感谢您的回复,但正如我在上面的 cmets 中所说,我 100% 确定 listItemsLayout 是问题所在。我现在将编辑所有内容,因为我知道其他布局不是问题
【解决方案2】:

对于任何有同样问题的人。我找到了修改膨胀 recyclerItems 布局方式的解决方案。

改变

View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_items, null);

View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_items, viewGroup, false);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-18
    • 2019-08-09
    • 2014-02-02
    • 1970-01-01
    相关资源
    最近更新 更多