【问题标题】:Android: Background image resize on keyboard pop upAndroid:键盘弹出时背景图像调整大小
【发布时间】:2015-06-18 08:12:47
【问题描述】:

我正在开发一个应用程序,其中背景图像在键盘弹出时缩小。我的 .xml 如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:facebook="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background" >

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

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

            /**
              Other stuff 
            */

        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

我在谷歌上搜索发现,添加

android:windowSoftInputMode="stateVisible|adjustPan"

在我的清单文件中。但是没用。

编辑:

键盘弹出前的布局如下:

然后弹出如下:

请检查背景图像的差异。在图像 1 中,图像的大小和在图像 2 中的背景图像缩小。我需要在弹出键盘时将页脚和背景向上移动。

我缺少什么或做错了什么,请建议我。

【问题讨论】:

  • adjustPan 应该完全满足您的需要(“活动的主窗口不会调整大小以为软键盘腾出空间。而是自动平移窗口的内容” i>),请参阅docs。你有什么问题?
  • 这个问题与this重复。
  • 为什么不把图片放在滚动视图的背景上呢?或者创建一个没有关系的图像视图来填充整个屏幕空间并将其设置为背景,因为它是一个 relatvielayout。
  • 请附上您的问题的截图。

标签: android android-layout android-scrollview android-background


【解决方案1】:

只需在您的onCreate() 中使用此代码:

protected void onCreate(Bundle savedInstanceState) {
...   
 getWindow().setBackgroundDrawableResource(R.drawable.your_image_resource);
...
}

并在您的 xml 中消除这一行:

android:background="@drawable/background"

阅读更多:

http://developer.android.com/reference/android/view/Window.html

【讨论】:

  • @ManojFegde 你有预付款吗?
  • 感谢您的建议,抱歉回复晚了。我试过你的答案。它可以工作,但我需要的是背景图像应该在键盘上向上移动,而不是隐藏在它后面。
  • @ManojFegde 你能用你想要什么的图片来编辑你的问题吗?
  • 如果我想在这个中使用比例类型怎么办?因为对于缺口屏幕图像正在拉伸。
  • @Nil 我不知道我在过去 3 年里没有使用过 Android。
【解决方案2】:

嘿,你可以试试添加这个

android:isScrollContainer="false" 

在您的 ScrollView 中。它曾经解决了我的问题。也可以帮到你。

另外,将此添加到 manifest.xml 中的活动中

android:windowSoftInputMode="adjustPan"

希望对您有所帮助..!! :)

【讨论】:

    【解决方案3】:

    我曾经遇到过类似的问题,我的问题是通过在清单中使用以下属性解决的,在声明活动的地方使用它。

     android:windowSoftInputMode="stateHidden|adjustResize|adjustPan"
    

    【讨论】:

    • 顺便说一句,如果您不想在活动开始时隐藏键盘,您可以跳过 stateHidden 部分
    【解决方案4】:

    好的,如果我的问题没有错,您需要一个带有背景和滚动视图的布局。当软键盘弹出时,您想调整滚动视图的大小,但将背景保持在全尺寸,对吗? 如果这是您想要的,那么我可能已经找到了解决此问题的方法:

    可以做的是2个活动。

    活动 1:

    public class StartActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        Intent StartApp = new Intent(this, DialogActivity.class);
        startActivity(StartApp);         // Launch your official (dialog) Activity
    
        setContentView(R.layout.start);  // your layout with only the background 
        }
    }
    

    活动 2:

    public class DialogActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);  // get rid of dimming
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);  //get rid of title bar (if you want)
    
        setContentView(R.layout.dialog);  //Dialog Layout with scrollview and stuff
    
        Drawable d = new ColorDrawable(Color.BLACK);  //make dialog transparent
        d.setAlpha(0);
        getWindow().setBackgroundDrawable(d);
        }
    }
    

    开始布局:

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

    对话框布局:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">
    
        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
    
                <LinearLayout
                    android:orientation="vertical"     
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
    
                    <!-- All your Scrollview Items -->
    
                </LinearLayout>
         </ScrollView>
    </RelativeLayout>
    

    AndroidManifest.xml

    开始活动:

    android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
    

    对话活动

    android:theme="@android:style/Theme.Dialog"
    android:windowSoftInputMode="adjustResize"
    android:excludeFromRecents="true"
    

    编辑: 要立即完成活动,请在 DialogActivity 中的某处使用以下内容(例如:覆盖后退按钮):

    @Override
    public void onBackPressed() {
        Intent intent = new Intent(getApplicationContext(), StartActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("EXIT", true);
        startActivity(intent);
    }
    

    onCreate()的StartActivity中:

    if (getIntent().getBooleanExtra("EXIT", false)) {
        finish();
    }
    else {
        Intent StartApp = new Intent(this, TestActivity.class);
        startActivity(StartApp);
    }
    

    截图

    正常

    单击了 EditText 框

    向下滚动后(ps:我将文本框放在滚动视图中,这就是它消失的原因;))

    我希望这会对你有所帮助;)

    【讨论】:

      【解决方案5】:

      我遇到了同样的问题。

      在 onCreate 方法中添加您的外部布局背景,如下所示

      getWindow().setBackgroundDrawableResource(R.drawable.login_bg);
      

      并将您的外部布局添加为

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/activity_main"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_weight="1"
      >
      <ScrollView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="#4d000000"
          ><other code></ScrollView></RelativeLayout>
      

      为外部布局添加android:layout_weight="1",并且不要在xml文件中设置背景 我认为这对某人有帮助

      【讨论】:

        【解决方案6】:

        您应该将android:background="@drawable/background" 移动到ImageView 上方的ScrollView。当键盘弹出时,它有效地使屏幕变小,并且将图像作为背景,您无法控制它的大小/裁剪方式。 因此,假设您希望图像全宽但保持纵横比,请尝试以下操作:

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:facebook="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        
            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:src="@drawable/background"
                android:scaleType="centerCrop" />
        
            <ScrollView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >
                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" >           
                    /**
                    Other stuff
                    */
                </RelativeLayout>
            </ScrollView>
        </RelativeLayout>
        

        您可以尝试不同的android:scaleType 值来达到您想要的效果。如果您希望从顶部裁剪图像而不是默认的中间裁剪,请参阅here 了解如何实现这一点,或尝试使用this

        【讨论】:

          【解决方案7】:

          检查this answer,基本技巧是您不要将背景设置为布局,而是设置为窗口。这可以使用getWindow().setBackgroundDrawable() 在活动中完成,或者您可以将其设置在活动的主题中。

          【讨论】:

          • 但是如何设置scaleType?
          【解决方案8】:

          您应该改为android:windowSoftInputMode="stateVisible|adjustResize",而不是android:windowSoftInputMode="stateVisible|adjustPan"。它对我有用。

          【讨论】:

          • 我试过这个但它不起作用,因为在搜索时我知道这是由于我的布局中的滚动视图。
          • 可能是因为你的AndroidManifest.xml,你能把你的manifest代码复制粘贴到这里吗?
          【解决方案9】:

          使用 ScrollView 作为顶级父级。 打开和关闭键盘时,它会自动上下滚动。

          【讨论】:

            【解决方案10】:

            你为什么不这样做

                <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:fillViewport="true"
                    android:focusable="true"
                    android:background="@drawable/background"
                    android:focusableInTouchMode="true">
            
                    <RelativeLayout
                        android:id="@+id/relative"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:focusable="true"
                        android:focusableInTouchMode="true"
                /**
                          Other stuff 
                        */
                >
              </RelativeLayout>
              </ScrollView>
            

            【讨论】:

              【解决方案11】:

              您应该在AndroidManifest.xml 文件中对ActivitywindowSoftInputMode 设置使用adjustResize 选项。

              【讨论】:

                【解决方案12】:

                我正在使用下面的视图层次结构,它对我来说工作正常:

                <?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" >
                
                    <ScrollView
                        android:id="@+id/rfqItemsParentScrollView"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" >
                
                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical" >
                

                【讨论】:

                • 它没用。背景也在缩小。
                • 我在询问背景图片。您没有提到图像及其缩小。
                猜你喜欢
                • 2011-07-15
                • 2017-02-25
                • 2019-11-17
                • 1970-01-01
                • 2015-07-05
                • 2011-05-16
                • 1970-01-01
                • 1970-01-01
                • 2018-05-14
                相关资源
                最近更新 更多