2017-10-04 57 views
0

如何使用工具欄和ScrollView?如何使用工具欄和ScrollView?

我有代碼:

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

    <android.support.v7.widget.Toolbar 
     android:id="@+id/my_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     android:elevation="4dp"/> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

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

     <EditText 
      android:id="@+id/editText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:ems="10" 
      android:inputType="textMultiLine" 
      android:background="#000000"/> 
     </LinearLayout> 
    </ScrollView> 
</LinearLayout> 

但這沒有顯示的EditText。我應該糾正什麼?

我不必使用LinearLayout - 我可以改變它。我只能使用ScrollView和工具欄,但這些都是我的嘗試。從horizontalLinearLayout

+0

使用'FrameLayout'而不是最'LinearLayout'的? –

回答

0

改變方向,以verticalandroid:orientation="vertical"

+0

我仍然看不到EditText。 – diker

0

我會使用兩種:

一個相對佈局:

<?xml version="1.0" encoding="utf-8"?> 
<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"> 

    <android.support.v7.widget.Toolbar 
    android:id="@+id/my_toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:layout_alignParentTop="true" 
    android:background="?attr/colorPrimary" 
    android:elevation="4dp" /> 

    <ScrollView 
    android:id="@+id/scroll_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/my_toolbar"> 

    <LinearLayout 
     android:id="@+id/scrollable_content_container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <EditText 
     android:id="@+id/editText" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#000000" 
     android:ems="10" 
     android:inputType="textMultiLine" 
     tools:text="hello world" /> 
    </LinearLayout> 
    </ScrollView> 
</RelativeLayout> 

或ConstraintLayout(我更喜歡這一個):

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
    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.support.v7.widget.Toolbar 
    android:id="@+id/my_toolbar" 
    android:layout_width="0dp" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    android:elevation="4dp" 
    app:layout_constraintEnd_toEndOf="parent" 
    app:layout_constraintStart_toStartOf="parent" 
    app:layout_constraintTop_toTopOf="parent" /> 

    <ScrollView 
    android:id="@+id/scroll_view" 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintEnd_toEndOf="parent" 
    app:layout_constraintStart_toStartOf="parent" 
    app:layout_constraintTop_toBottomOf="@+id/my_toolbar"> 

    <LinearLayout 
     android:id="@+id/scrollable_content_container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <EditText 
     android:id="@+id/editText" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#000000" 
     android:ems="10" 
     android:inputType="textMultiLine" 
     tools:text="hello world" /> 
    </LinearLayout> 
    </ScrollView> 
</android.support.constraint.ConstraintLayout> 

現在你可以看到它:

Preview