【问题标题】:Want to add a scroll view想要添加滚动视图
【发布时间】:2015-08-26 17:33:58
【问题描述】:

当我尝试添加ScrollView 时,它给了我一个错误,即滚动视图只能包含一个孩子。那么如何添加 ScrollView 呢? 而且当我在各种手机中打开我的应用程序时,所有手机都会给出不同的布局。如何为所有移动设备管理相同的应用外观?

注册.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="match_parent"
    android:layout_height="match_parent"
    android:background="#0066CC"
    android:orientation="vertical"

    android:padding="10dp" >

    <ImageView
        android:id="@+id/upImage"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:background="#000"
        android:layout_gravity="center_horizontal"/>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="465dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:layout_weight="0.02"
        android:layout_marginBottom="100dp"
        android:textSize="15sp"
        android:textStyle="bold"
        android:text="Tap to upload Profile Picture" />

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

     android:layout_marginBottom="50dp"
     android:hint="Enter name of the Image"
        android:ems="10" >


    </EditText>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name" />

    <EditText
        android:id="@+id/etName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Age" />

    <EditText
        android:id="@+id/etAge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Username" />

    <EditText
        android:id="@+id/etUsername"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Password" />

    <EditText
        android:id="@+id/etPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/bRegister"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Register" />

</LinearLayout>

【问题讨论】:

    标签: android xml android-scrollview


    【解决方案1】:

    ScrollView 只允许独生子女 所以把你的父母LinearLayout放在ScrollView里面,如下所示

    <ScrollView 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">
    
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#0066CC"
            android:orientation="vertical"
            android:padding="10dp" >
    
            <ImageView
                android:id="@+id/upImage"
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" 
                android:background="#000"
                android:layout_gravity="center_horizontal"/>
    
            <TextView
                android:id="@+id/textView1"
                android:layout_width="465dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:gravity="center_horizontal"
                android:layout_weight="0.02"
                android:layout_marginBottom="100dp"
                android:textSize="15sp"
                android:textStyle="bold"
                android:text="Tap to upload Profile Picture" />
    
            <EditText
                android:id="@+id/imName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="50dp"
                android:hint="Enter name of the Image"
                android:ems="10" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Name" />
    
            <EditText
                android:id="@+id/etName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Age" />
    
            <EditText
                android:id="@+id/etAge"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Username" />
    
            <EditText
                android:id="@+id/etUsername"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Password" />
    
            <EditText
                android:id="@+id/etPassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:inputType="textPassword" />
    
            <Button
                android:id="@+id/bRegister"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Register" />
    
        </LinearLayout>
    </ScrollView>
    

    【讨论】:

      【解决方案2】:

      Android 仅支持 ScrollView 内的单个子级。最简单的方法是将所有内容包含在LinearLayout 中,并使其成为ScrollView 的子代。

      如果您希望在整个应用程序中保持相同的外观和感觉,请尝试使用RelativeLayout 而不是LinearLayout 作为ScrollView 的子代。

      【讨论】:

        猜你喜欢
        • 2016-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-06
        • 2015-05-15
        • 2014-09-10
        相关资源
        最近更新 更多