使布局可滚动

时间:2014-11-22 01:45:29

标签: android android-layout android-linearlayout scrollable android-relativelayout

我有这种不可滚动的布局

我尝试了很多解决方案并阅读了很多问题和答案,但没有任何关系。

<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: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.twaa9l.chates.MyProfile"
    android:weightSum="2"
    android:scrollbars="horizontal"
    android:orientation="vertical" >


    <RelativeLayout
        android:layout_weight="1.5"   
        android:layout_width="match_parent"
        android:layout_height="0dp">

            <ImageView
                android:id="@+id/profilePicture" 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/balloon"
                android:scaleType="fitXY"
                android:contentDescription="Main Image"
                android:padding="1dp"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="image"
                android:clickable="true"
                android:padding="4dp"
                android:layout_margin="8dp"
                android:scaleType="fitXY"
                android:background="@drawable/edit_states"
                android:src="@drawable/ic_action_edit"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"/>
    </RelativeLayout>

<!-- Full Name -->
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="0.25"
        android:layout_marginTop="@dimen/activity_vertical_margin">

       <EditText 
            android:id="@+id/fullName"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/textview_border"
            android:hint="Enter sample text..."
            android:paddingTop="6dp"
            android:paddingBottom="8dp"
            android:paddingLeft="6dp"
            android:textSize="18sp"
            android:gravity="center_vertical"/>

       <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="Edit image icon"
            android:clickable="true"
            android:padding="4dp"
            android:layout_margin="8dp"
            android:scaleType="fitXY"
            android:background="@drawable/edit_states"
            android:src="@drawable/ic_action_edit"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"/>
    </RelativeLayout>


    <!-- Email -->
        <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="0.25"
        android:layout_marginTop="@dimen/activity_vertical_margin">

       <EditText 
            android:id="@+id/emailAddress"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/textview_border"
            android:hint="Enter sample text..."
            android:paddingTop="6dp"
            android:paddingBottom="8dp"
            android:paddingLeft="6dp"
            android:textSize="18sp"
            android:gravity="center_vertical"/>

       <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="Edit image icon"
            android:clickable="true"
            android:padding="4dp"
            android:layout_margin="8dp"
            android:scaleType="fitXY"
            android:background="@drawable/edit_states"
            android:src="@drawable/ic_action_edit"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"/>
    </RelativeLayout>


<!-- About You -->  
        <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="0.25"
        android:layout_marginTop="@dimen/activity_vertical_margin">

       <EditText 
            android:id="@+id/aboutUser"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/textview_border"
            android:hint="Enter sample text..."
            android:paddingTop="6dp"
            android:paddingBottom="8dp"
            android:paddingLeft="6dp"
            android:textSize="18sp"
            android:gravity="center_vertical"/>

       <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="Edit image icon"
            android:clickable="true"
            android:padding="4dp"
            android:layout_margin="8dp"
            android:scaleType="fitXY"
            android:background="@drawable/edit_states"
            android:src="@drawable/ic_action_edit"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"/>
    </RelativeLayout>


    <!-- Password -->
        <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="0.25"
        android:layout_marginTop="@dimen/activity_vertical_margin">

       <EditText 
            android:id="@+id/newPassword"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/textview_border"
            android:hint="Enter sample text..."
            android:paddingTop="6dp"
            android:paddingBottom="8dp"
            android:paddingLeft="6dp"
            android:textSize="18sp"
            android:gravity="center_vertical"/>

       <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="Edit image icon"
            android:clickable="true"
            android:padding="4dp"
            android:layout_margin="8dp"
            android:scaleType="fitXY"
            android:background="@drawable/edit_states"
            android:src="@drawable/ic_action_edit"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"/>
    </RelativeLayout>

        <TextView
            android:id="@+id/lastUpdate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

</LinearLayout>

我尝试使用ScrollView ,但我在LogCat中遇到此错误

ScrollView can host only one direct child

所以我正在寻找另一种解决方案,可能是通过活动中的代码或其他东西。

screenshot

1 个答案:

答案 0 :(得分:3)

您应该将内容放在 ScrollView 中,就像这样

<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: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.twaa9l.chates.MyProfile">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <RelativeLayout
                android:layout_weight="1.5"   
                android:layout_width="match_parent"
                android:layout_height="0dp">
                ...
            </RelativeLayout>
            ...
        </LinearLayout>
    </ScrollView>
</LinearLayout>