【问题标题】:where can I add include in my xml file?我在哪里可以在我的 xml 文件中添加包含?
【发布时间】:2017-03-12 08:42:45
【问题描述】:

我正在尝试在我的 xml 文件中添加一个工具栏。

所以我制作了toolbar.xml,我需要将它包含在upload.xml中:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_upload"
    android:layout_width="match_parent"
    android:padding="10dp"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.sk.mf.Upload"
    android:background="#fff">

    <LinearLayout

        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/imageToUpload"
            android:layout_gravity="center_horizontal"
            android:layout_width="150dp"
            android:layout_height="150dp" />

        <EditText
            android:id="@+id/etUploadName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/bUploadImage"
            android:text="Upload Image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


    </LinearLayout>


</ScrollView>

问题是,在我放置它的任何地方,我都会遇到如下错误: 滚动视图只能托管一个直接子级(在滚动视图内) 多个根标签(在滚动视图之后) ...

我的问题是,我可以在哪里以及如何将下面的代码放在上面的 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">
    <include android:id="@+id/toolbar" layout="@layout/toolbar"></include>
</RelativeLayout>

【问题讨论】:

    标签: android xml android-studio android-xml android-styles


    【解决方案1】:

    试试这个:用LinearLayouttoolbar 标签包裹你的布局

     <?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="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar"></include>
    
    
        <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/activity_upload"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#fff"
            android:padding="10dp"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            tools:context="com.sk.mf.Upload">
    
            <LinearLayout
    
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
                <ImageView
                    android:id="@+id/imageToUpload"
                    android:layout_width="150dp"
                    android:layout_height="150dp"
                    android:layout_gravity="center_horizontal" />
    
                <EditText
                    android:id="@+id/etUploadName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
    
                <Button
                    android:id="@+id/bUploadImage"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Upload Image" />
    
            </LinearLayout>
    
        </ScrollView>
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-21
      • 2012-07-17
      • 1970-01-01
      • 2019-07-24
      • 1970-01-01
      • 2010-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多