android - 我想将 Scrollview 添加到 Button?

标签 android xml

我想将 ScrollView 添加到相关布局,因为当我增加按钮并在我的手机上运行它时我有更多按钮我看不到一些按钮所以我想为按钮添加 ScrollView 。我怎样才能添加它? 下面是我的xml代码

<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: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=".MainActivity" 
        android:background="@drawable/background1">

      <include layout="@layout/header" />

        <include layout="@layout/footer" />

        <RelativeLayout 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/header"
            android:layout_above="@+id/footer"
    >

                <Button 
                    android:id="@+id/templesbtn"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:text="Historical"
                    style="?android:attr/buttonStyleSmall"
                    android:background="@drawable/mymenubuttons"
                     android:layout_marginBottom="10dp"
                     android:layout_marginTop="10dp"/>
                <Button 
                    android:id="@+id/resortsbtn"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:text="Resorts"
                    style="?android:attr/buttonStyleSmall"
                    android:background="@drawable/mymenubuttons"
                     android:layout_marginBottom="10dp"
                    android:layout_below="@+id/templesbtn"/>
                <Button 
                    android:id="@+id/shoppingmallsbtn"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:text="Shopping"
                    style="?android:attr/buttonStyleSmall"
                    android:background="@drawable/mymenubuttons"
                    android:layout_marginBottom="10dp"
                    android:layout_below="@+id/resortsbtn"/>
                <Button 
                    android:id="@+id/theatersbtn"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:text="Theaters"
                    style="?android:attr/buttonStyleSmall"
                    android:background="@drawable/mymenubuttons"
                     android:layout_marginBottom="10dp"
                    android:layout_below="@+id/shoppingmallsbtn"/>
                <Button 
                    android:id="@+id/restaurantsbtn"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:text="Restaurants"
                    style="?android:attr/buttonStyleSmall"
                    android:background="@drawable/mymenubuttons"
                    android:layout_marginBottom="10dp"
                    android:layout_below="@+id/theatersbtn"
                    />

                <Button 
                    android:id="@+id/hospitalbtn"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:text="Hospitals"
                    style="?android:attr/buttonStyleSmall"
                    android:background="@drawable/mymenubuttons"
                    android:layout_marginBottom="10dp"
                    android:layout_below="@+id/restaurantsbtn"
                    />

                  <Button 
                    android:id="@+id/collegebtn"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:text="Colleges"
                    style="?android:attr/buttonStyleSmall"
                    android:background="@drawable/mymenubuttons"
                    android:layout_marginBottom="10dp"
                    android:layout_below="@+id/hospitalbtn"
                    />
                   <Button 
                    android:id="@+id/onlineservicebtn"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:text="Online Services"
                    style="?android:attr/buttonStyleSmall"
                    android:background="@drawable/mymenubuttons"
                    android:layout_marginBottom="10dp"
                    android:layout_below="@+id/collegebtn"
                    />



      <RelativeLayout
          android:layout_toRightOf="@+id/templesbtn" 
          android:layout_width="match_parent"
        android:layout_height="wrap_content">

   <GridView
       android:id="@+id/gridView1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
       android:layout_alignParentTop="true"
       android:numColumns="2"
       android:visibility="invisible" >
   </GridView>

 </RelativeLayout>

   </RelativeLayout>


</RelativeLayout>

谁能帮帮我?

最佳答案

首先用线性布局(垂直)包裹所有按钮,然后用 ScrollView 包裹:

     <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <LinearLayout

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
<Button 
                android:id="@+id/templesbtn"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Historical"
                style="?android:attr/buttonStyleSmall"
                android:background="@drawable/mymenubuttons"
                 android:layout_marginBottom="10dp"
                 android:layout_marginTop="10dp"/>
            <Button 
                android:id="@+id/resortsbtn"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Resorts"
                style="?android:attr/buttonStyleSmall"
                android:background="@drawable/mymenubuttons"
                 android:layout_marginBottom="10dp"
                android:layout_below="@+id/templesbtn"/>
            <Button 
                android:id="@+id/shoppingmallsbtn"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Shopping"
                style="?android:attr/buttonStyleSmall"
                android:background="@drawable/mymenubuttons"
                android:layout_marginBottom="10dp"
                android:layout_below="@+id/resortsbtn"/>
            <Button 
                android:id="@+id/theatersbtn"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Theaters"
                style="?android:attr/buttonStyleSmall"
                android:background="@drawable/mymenubuttons"
                 android:layout_marginBottom="10dp"
                android:layout_below="@+id/shoppingmallsbtn"/>
            <Button 
                android:id="@+id/restaurantsbtn"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Restaurants"
                style="?android:attr/buttonStyleSmall"
                android:background="@drawable/mymenubuttons"
                android:layout_marginBottom="10dp"
                android:layout_below="@+id/theatersbtn"
                />

            <Button 
                android:id="@+id/hospitalbtn"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Hospitals"
                style="?android:attr/buttonStyleSmall"
                android:background="@drawable/mymenubuttons"
                android:layout_marginBottom="10dp"
                android:layout_below="@+id/restaurantsbtn"
                />

              <Button 
                android:id="@+id/collegebtn"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Colleges"
                style="?android:attr/buttonStyleSmall"
                android:background="@drawable/mymenubuttons"
                android:layout_marginBottom="10dp"
                android:layout_below="@+id/hospitalbtn"
                />
               <Button 
                android:id="@+id/onlineservicebtn"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Online Services"
                style="?android:attr/buttonStyleSmall"
                android:background="@drawable/mymenubuttons"
                android:layout_marginBottom="10dp"
                android:layout_below="@+id/collegebtn"
                />

        </LinearLayout>
    </ScrollView>


    <RelativeLayout
        android:layout_toRightOf="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <GridView
            android:id="@+id/gridView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:numColumns="2"
            android:visibility="invisible"></GridView>

    </RelativeLayout>

</RelativeLayout>

关于android - 我想将 Scrollview 添加到 Button?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33626996/

相关文章:

javascript - 悬停和激活时 ionic 更改侧面菜单图标

安卓无法浏览内网

javascript - 来自 Android 浏览器和 Dashcode 移动项目的错误触摸事件

java - 如何在 Spring Boot 中将所有常见依赖项从 Maven 模块移至父模块

python - 防止 ElementTree 为每个元素添加命名空间

c# - 使用 C# 在 XML 中获取具有特定名称的标签

java - 如何更改android studio中的编码

android - 如何将对象传递给另一个 Activity ?

C# 无法通过 dataFieldName 获取 xml

java - Maven 不从 test.xml 读取参数