【问题标题】:Using ScrollView with LinearLayout, Android将 ScrollView 与 LinearLayout 一起使用,Android
【发布时间】:2014-11-02 20:12:10
【问题描述】:

在我的 xml 文件中,我有一个线性布局和一个按钮。 linearLayout 包含很多 textView 和复选框。所以我希望这些 textViews 和复选框能够滚动,但按钮应该保留在其位置,即它不应该与 textViews 一起滚动。为此,我用这样的滚动视图覆盖我的线性布局

<?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"
    android:orientation="vertical"
    android:background="@drawable/homeo11" >
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
          <LinearLayout 
             android:id="@+id/nexttodetail"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:orientation="vertical" 
             android:layout_marginTop="10dp">

          </LinearLayout>
    </ScrollView>>

   <Button   android:id="@+id/Next4"
            android:layout_gravity="right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Next" />

 </LinearLayout>

但是这样做,隐藏了我的按钮。意味着文本视图和复选框现在可以正确滚动,但我看不到我的下一个按钮。我尝试将 scrollView 布局:从 fill_parent 替换为 wrap_content 的高度,但这也不起作用。有什么帮助吗?

【问题讨论】:

  • 在滚动条和按钮中使用android:layout_weight="1"
  • 如果您改用RelativeLayout 作为最外层布局,您可以在按钮内使用android:layout_alignParentBottom="true" 将其保持在底部并始终可见。

标签: android android-layout scrollview


【解决方案1】:

使用 RelativeLayout 作为您的根视图,然后使用 align_above、align_parent_top 和 align_parent_bottom 设置您的组件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/homeo11" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"      
        android:layout_above="@+id/Next4" >       
            <LinearLayout 
                android:id="@+id/nexttodetail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" 
                android:layout_marginTop="10dp">

                    (...)

            </LinearLayout>
    </ScrollView>>

   <Button  
       android:id="@+id/Next4"
       android:layout_gravity="right"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"     
       android:layout_marginTop="10dp"
       android:text="Next" />

 </RelativeLayout>

【讨论】:

  • 试过你的代码,但用这个隐藏了我的线性布局!
猜你喜欢
  • 1970-01-01
  • 2016-12-12
  • 2012-06-14
  • 2018-04-23
  • 2012-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-19
相关资源
最近更新 更多