当在内容布局中包含LinearLayout时,LinearLayout没有显示出来。

3

我有一个activity_pic.xml布局文件,其中包含content_pic.xml来定义我的应用程序的布局。以下是activity_pic.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.olivia.myapplication.PicActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <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_pic" />

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

content_pic.xml

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

    <Button
        android:id="@+id/pick_image_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_text"/>

    <ImageView
        android:id="@+id/image_view"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>

</LinearLayout>

当我在AppBarLayout中包含这一行<include layout="@layout/content_pic" />时(即将它向上移动两行),按钮就会出现,按钮的所有功能都可以正常使用。但是,当我把它放在后面时,按钮就不会出现,我只能看到布局的“应用栏”部分。

好的,下面的图片展示了我想要的外观(以及设计视图显示的content_pic.xml)

content_pic design view

这是它实际的样子

actual app

然而,当我将<include layout="@layout/content_pic" />放入AppBarLayout中时,我可以看到按钮,如下图所示。

content in AppBarLayout

我已经尝试将content_pic.xml的内容直接包含,而不是使用include语句,但仍然没有出现。

我已经想不出别的办法了 - 有人能建议可能出了什么问题吗?


你能提供截图和更多信息吗?我不明白问题出在哪里。 - Isaac Urbina
你想把内容文件放在应用栏布局之外吗? - Bhunnu Baba
是的,我希望按钮出现在应用栏布局之外,但当我将其放到外面时,它不会显示。我已经提供了更多照片来说明问题。 - Olivia Wilson
1个回答

0

AppBarLayout 在使用 NestedScrollViewRecyclerView 时效果良好,因此我建议您将 content_pic.xml 更改为以下内容。

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
      <LinearLayout 
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

       <Button
            android:id="@+id/pick_image_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_text"/>

       <ImageView
            android:id="@+id/image_view"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>

     </LinearLayout>

I hope this will Help.

EDIT

You Just need to Change your content_pic.xml to this

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"

     <!-- Importent -->
     xmlns:app="http://schemas.android.com/apk/res-auto"
     app:layout_behavior="@string/appbar_scrolling_view_behavior"

     android:layout_width="fill_parent"
     android:layout_height="fill_parent" >

          <LinearLayout
               xmlns:tools="http://schemas.android.com/tools"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:orientation="vertical" >

          <Button
               android:id="@+id/pick_image_button"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="Hello" />

          <ImageView
               android:id="@+id/image_view"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content" />
     </LinearLayout>
 </android.support.v4.widget.NestedScrollView>


嗨,我尝试过这样做,但似乎没有帮助,仍然无法显示。如果有帮助的话,我已经添加了我想要发生的事情的照片以及可行和不可行的事情。 - Olivia Wilson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接