【问题标题】:Fix background image on Scrollview Android修复 Scrollview Android 上的背景图像
【发布时间】:2014-03-17 16:39:22
【问题描述】:

当我在代码中使用 Scrollview 时,我的背景图像会通过按钮进行拉伸和滚动。我只想要按钮滚动,而不是背景。我附上了一张相关的图片,你可以弄清楚。 这是 XML 文件:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:background="@drawable/back"
                  android:orientation="vertical" >


        <ImageButton
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/app"
                android:layout_gravity="center"
                android:background="@drawable/bluebutton"
                android:layout_marginTop="80dp"
                android:layout_marginRight="80dp"
                android:layout_marginLeft="80dp"

                />

        <ImageButton
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/moshiri"
                android:background="@drawable/bluebutton"
                android:layout_marginTop="8dp"
                android:layout_marginRight="80dp"
                android:layout_marginLeft="80dp"
                />
        <ImageButton
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"

                android:id="@+id/about"
                android:background="@drawable/bluebutton"
                android:layout_marginTop="8dp"
                android:layout_marginRight="80dp"
                android:layout_marginLeft="80dp"
                />
        <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:id="@+id/exit"
                android:background="@drawable/bluebutton"
                android:layout_marginTop="8dp"
                android:layout_marginRight="80dp"
                android:layout_marginLeft="80dp"
                />

    </LinearLayout>
</ScrollView>

【问题讨论】:

    标签: android layout scrollview


    【解决方案1】:

    这东西对我来说很好用。

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
            <ImageView
                android:id="@+id/gearImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:clickable="true"
                android:src="@drawable/imageName" />
    
    </ScrollView>
    

    【讨论】:

      【解决方案2】:

      在 ScrollView 上设置背景图片,并将 Padding 添加到 ScrollView 或将 Margins 添加到 Linearlayout。

      【讨论】:

      • 这可行,但要小心,因为滚动视图性能对图像大小很敏感
      【解决方案3】:

      在根目录而不是滚动视图中创建 1 个线性布局,并将 use android:background="@drawable/back" 设置为该布局

      <LinearLayout 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:background="@drawable/back" >
      
          <ScrollView
              android:layout_width="match_parent"
              android:layout_height="match_parent" >
      
              <LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:background="@drawable/back"
                  android:orientation="vertical" >
      
      
                  <ImageButton
                      android:id="@+id/app"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:layout_gravity="center"
                      android:layout_marginLeft="80dp"
                      android:layout_marginRight="80dp"
                      android:layout_marginTop="80dp"
                      android:background="@drawable/bluebutton" />
      
                  <ImageButton
                      android:id="@+id/moshiri"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:layout_marginLeft="80dp"
                      android:layout_marginRight="80dp"
                      android:layout_marginTop="8dp"
                      android:background="@drawable/bluebutton" />
      
                  <ImageButton
                      android:id="@+id/about"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:layout_marginLeft="80dp"
                      android:layout_marginRight="80dp"
                      android:layout_marginTop="8dp"
                      android:background="@drawable/bluebutton" />
      
                  <ImageButton
                      android:id="@+id/exit"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_marginLeft="80dp"
                      android:layout_marginRight="80dp"
                      android:layout_marginTop="8dp"
                      android:background="@drawable/bluebutton" />
              </LinearLayout>
          </ScrollView>
      
      
      </LinearLayout>
      

      这里我假设你的@drawable/back 是你给定的图像背景

      【讨论】:

      • 我想要按钮滚动。
      • 背景拉伸和滚动。没有任何改变。
      • 没有区别。还在拉伸
      【解决方案4】:

      这对我有用。图像自动调整大小并填充整个背景(调整宽度并裁剪高度。)

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content">
      
          <RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent" >
      
              <ImageView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:scaleType="centerCrop"
                  android:src="@drawable/app_back_image" />
          </RelativeLayout>
      
          <ScrollView
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:isScrollContainer="true">
              ...
          </ScrollView>
      </RelativeLayout>
      

      【讨论】:

        【解决方案5】:

        正如 Raanan 所说,在 ScrollView 上设置背景图像,但为了防止背景在不同的屏幕尺寸上拉伸,您可以为其添加重力。

        <?xml version="1.0" encoding="utf-8"?>
        <bitmap
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/back"
        android:gravity="top" />
        

        【讨论】:

        • 那个代码是干什么用的?我在布局中看不到任何内容,并且应用程序崩溃了。
        【解决方案6】:

        这样做,

        <?xml version="1.0" encoding="utf-8"?>
        
        <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="match_parent"
                    android:background="@drawable/back"
                    android:layout_height="match_parent">
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                          android:layout_width="fill_parent"
                          android:layout_height="fill_parent"
                          android:orientation="vertical" >
        
        
                <ImageButton
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/app"
                        android:layout_gravity="center"
                        android:background="@drawable/bluebutton"
                        android:layout_marginTop="80dp"
                        android:layout_marginRight="80dp"
                        android:layout_marginLeft="80dp"
        
                        />
        
                <ImageButton
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/moshiri"
                        android:background="@drawable/bluebutton"
                        android:layout_marginTop="8dp"
                        android:layout_marginRight="80dp"
                        android:layout_marginLeft="80dp"
                        />
                <ImageButton
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
        
                        android:id="@+id/about"
                        android:background="@drawable/bluebutton"
                        android:layout_marginTop="8dp"
                        android:layout_marginRight="80dp"
                        android:layout_marginLeft="80dp"
                        />
                <ImageButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
        
                        android:id="@+id/exit"
                        android:background="@drawable/bluebutton"
                        android:layout_marginTop="8dp"
                        android:layout_marginRight="80dp"
                        android:layout_marginLeft="80dp"
                        />
        
            </LinearLayout>
        </ScrollView>
        

        【讨论】:

          【解决方案7】:
          <?xml version="1.0" encoding="utf-8"?>
          <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              tools:context=".CadetEnrollmentFormActivity">
          
              <ImageView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="@color/white"
                  android:src="@drawable/background" />
          
              <ScrollView
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content">
                  <LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:orientation="vertical">
          <!--put your view here -->
          
          
                      </LinearLayout>
                  </LinearLayout>
          
              </ScrollView>
          </FrameLayout>
          

          【讨论】:

          • 加点解释会更好
          • 欢迎来到 Stack Overflow!感谢您提供此代码 sn-p,它可能会提供一些有限的即时帮助。 proper explanation would greatly improve its long-term value 通过展示为什么这是一个很好的解决问题的方法,并且会使其对未来有其他类似问题的读者更有用。请edit您的回答添加一些解释,包括您所做的假设。
          【解决方案8】:

          您可以使用 LinearLayout 作为 ScrollView 的根元素。 然后将背景设置为 Linearlayout。

          <LinearLayout
              .......
              android:background="@drawable/background">
              
              <ScrollView
                  ........>
                  
                  <LinearLayout
                      .......>
                      //your code hear
                  <LinearLayout/>
              <ScrollView/>       
          <LinearLayout/>
          

          【讨论】:

            【解决方案9】:

            将清单添加到相应的活动中

            android:windowSoftInputMode="adjustResize" 
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2012-04-10
              • 2019-04-24
              • 1970-01-01
              • 1970-01-01
              • 2013-12-24
              • 2013-10-03
              • 1970-01-01
              相关资源
              最近更新 更多