2017-10-04 60 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