Android:弹出键盘上的背景图片大小调整

Man*_*gde 32 android android-layout android-scrollview android-background

我正在开发一个应用程序,其中背景图像在键盘弹出窗口缩小.我的.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>
Run Code Online (Sandbox Code Playgroud)

我在Google上搜索并发现,要添加

android:windowSoftInputMode="stateVisible|adjustPan"
Run Code Online (Sandbox Code Playgroud)

在我的清单文件中.但它没用.

编辑:

键盘弹出前我的布局如下:

图片1

弹出后像:

图2

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

我错过了什么或做错了请建议我.

Adr*_*uer 35

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

protected void onCreate(Bundle savedInstanceState) {
...   
 getWindow().setBackgroundDrawableResource(R.drawable.your_image_resource);
...
}
Run Code Online (Sandbox Code Playgroud)

并删除xml中的这一行:

android:background="@drawable/background"
Run Code Online (Sandbox Code Playgroud)

阅读更多:

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


mus*_*hid 10

我曾经遇到类似的问题,我的解决方案是使用清单中的以下属性来解决你的活动.

 android:windowSoftInputMode="stateHidden|adjustResize|adjustPan"
Run Code Online (Sandbox Code Playgroud)

  • scrollview剂量无法使用此解决方案:-( (2认同)

Str*_*der 7

好的,如果我的问题正确,你需要一个带背景和滚动视图的布局.当弹出软键盘时,您想要调整滚动视图的大小,但保持背景的大小正确吗?如果这是你想要的,我可能找到了解决这个问题的方法:

可以做的是做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 
    }
}
Run Code Online (Sandbox Code Playgroud)

活性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);
    }
}
Run Code Online (Sandbox Code Playgroud)

开始布局:

<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>
Run Code Online (Sandbox Code Playgroud)

对话框布局:

<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>
Run Code Online (Sandbox Code Playgroud)

AndroidManifest.xml中

开始活动:

android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
Run Code Online (Sandbox Code Playgroud)

对话框活动

android:theme="@android:style/Theme.Dialog"
android:windowSoftInputMode="adjustResize"
android:excludeFromRecents="true"
Run Code Online (Sandbox Code Playgroud)

编辑: 要立即完成活动,请在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);
}
Run Code Online (Sandbox Code Playgroud)

onCreate()StartActivity:

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

截图!

正常 正常

单击EditText框 点击文本框

向下滚动后(ps:我把文本框放在scrollview里面,这就是为什么它消失了;)) 滚下来之后

我希望这会帮助你;)


iMD*_*oid 6

嘿,你可以尝试添加这个

android:isScrollContainer="false" 
Run Code Online (Sandbox Code Playgroud)

在你的ScrollView中.它曾经解决了我的问题.也可以帮助你.

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

android:windowSoftInputMode="adjustPan"
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你..!!:)


小智 6

我遇到过同样的问题。

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

getWindow().setBackgroundDrawableResource(R.drawable.login_bg);
Run Code Online (Sandbox Code Playgroud)

并添加您的外部布局为

<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>
Run Code Online (Sandbox Code Playgroud)

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


Moh*_*sif -5

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

<?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" >
Run Code Online (Sandbox Code Playgroud)