【问题标题】:Create a LinearLayout inside a ScrollView在 ScrollView 中创建 LinearLayout
【发布时间】:2018-10-11 06:51:13
【问题描述】:

我已尝试创建附在下面的image。但由于某种原因,我的image 不在screen 的顶部,而我的text 不在彼此下方。我将如何解决这个问题?因为我是安卓新手。

我想在下面的图片中实现什么

代码如下

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:orientation="vertical"
    android:background="@color/colorPrimary"
    android:layout_gravity="center">

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


       <ImageView
           android:layout_gravity="center"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:src="@drawable/solver"/>

       <View
           android:layout_marginTop="5dp"
           android:layout_width="match_parent"
           android:layout_height="1dp"
           android:background="@android:color/darker_gray"/>

       <LinearLayout
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:orientation="horizontal"
           android:layout_gravity="center">

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

               <TextView
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:text="Record phone calls"
                   android:textColor="@color/colorWhite"
                   android:layout_gravity="center"
                   android:textSize="18sp"/>

               <TextView
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:text="Know who viewed your profile"
                   android:textColor="@color/colorWhite"
                   android:layout_gravity="center"
                   android:textSize="18sp"/>

               <TextView
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:text="Option to view profiles privately"
                   android:textColor="@color/colorWhite"
                   android:layout_gravity="center"
                   android:textSize="18sp"/>

               <TextView
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:text="Get the Premium badge on your profile"
                   android:textColor="@color/colorWhite"
                   android:layout_gravity="center"
                   android:textSize="18sp"/>

               <TextView
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:text="30 contact requests a month"
                   android:layout_gravity="center"
                   android:textColor="@color/colorWhite"
                   android:textSize="18sp"/>

               <TextView
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:text="No ads"
                   android:layout_gravity="center"
                   android:textColor="@color/colorWhite"
                   android:textSize="18sp"/>

           </LinearLayout>

       </LinearLayout>

       <View
           android:layout_marginTop="5dp"
           android:layout_width="match_parent"
           android:layout_height="1dp"
           android:background="@android:color/darker_gray"/>

       <Button
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="center"
           android:text="Monthly Premium      R19,19"/>

       <Button
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="center"
           android:text="Yearly Premium        R179,99"/>

   </LinearLayout>

</ScrollView>

【问题讨论】:

  • 首先将android:fillViewPort="true" 放入正确包含其子元素的ScrollView。根据您的设计使用ConstrainLayoutRelativeLayout 而不是LinearLayout
  • 从线性布局和滚动视图中删除android:layout_gravity="center"

标签: android android-layout android-linearlayout android-scrollview


【解决方案1】:

1) 将android:fillViewport="true" 添加到ScrollView

2) 与&lt;Space /&gt;一起玩

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:orientation="vertical"
    android:background="@color/colorPrimary"
    android:layout_gravity="center"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="vertical">
        <Space
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"/>
        <ImageView
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_delete"/>


        <Space
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"/>
        .
        .
        .

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Monthly Premium      R19,19"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Yearly Premium        R179,99"/>


        <Space
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    </LinearLayout>

</ScrollView>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多