Android:在键盘弹出窗口上调整背景图像大小
我正在开发一个应用程序,其中背景图像会在键盘弹出窗口上缩小。My.xml如下所示:Android:在键盘弹出窗口上调整背景图像大小,android,android-layout,android-scrollview,android-background,Android,Android Layout,Android Scrollview,Android Background,我正在开发一个应用程序,其中背景图像会在键盘弹出窗口上缩小。My.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
<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>
在我的清单文件中。但这没有用
编辑:
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
}
}
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>
键盘弹出前的布局如下所示:
在弹出后,如:
请检查背景图像中的差异。在图1中,图像大小为,在图2中,背景图像缩小。我需要在键盘弹出窗口上向上移动页脚和背景
我遗漏了什么或做错了什么,请建议我。我正在使用下面的视图继承权,它对我来说很好:
<?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" >
您应该改为android:windowSoftInputMode=“stateVisible | adjustPan”而不是android:windowSoftInputMode=“stateVisible | adjustResize”
。它对我有用。嘿,你能试着添加这个吗
android:isScrollContainer="false"
在滚动视图中。它曾经解决过我的问题。也许对你也有帮助
另外,将其添加到manifest.xml中的活动中
android:windowSoftInputMode="adjustPan"
希望能有帮助!!:) 您应该将android:background=“@drawable/background”
移动到ImageView
上方的ScrollView
中。当键盘弹出时,它有效地使屏幕变小,并且以图像作为背景,您无法控制如何调整/裁剪它的大小。
因此,假设您希望图像全宽,但保持Aspect比率,请尝试以下方法:
<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
值来达到您想要的效果。如果您希望从顶部而不是默认的中间裁剪图像,请参见如何实现此操作,或者尝试使用library确定如果我答对了您的问题,您需要具有背景和滚动视图的布局。当软键盘弹出时,您想调整滚动视图的大小,但要保持背景的全尺寸,对吗?
如果这是您想要的,那么我可能已经找到了解决此问题的方法:
你能做的就是做两件事
活动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
}
}
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>
活动2:
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
}
}
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>
开始布局:
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
}
}
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>
对话活动
android:theme="@android:style/Theme.Dialog"
android:windowSoftInputMode="adjustResize"
android:excludeFromRecents="true"
编辑:
要立即完成活动,请在DialogActivity中的某个位置使用以下命令(例如:覆盖backbutton):
在StartActivity的onCreate()
中:
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
else {
Intent StartApp = new Intent(this, TestActivity.class);
startActivity(StartApp);
}
屏幕截图
正常
单击编辑文本框
向下滚动后(注:我将文本框放在scrollview中,这就是它消失的原因;)
我希望这能帮助您解决问题;)检查,基本技巧是不将背景设置为布局,而是设置为窗口。这可以通过使用getWindow().setBackgroundDrawable()
从活动中完成,也可以在活动的主题中设置它。只需在onCreate()中使用此代码:
protected void onCreate(Bundle savedInstanceState) {
...
getWindow().setBackgroundDrawableResource(R.drawable.your_image_resource);
...
}
并删除xml中的这一行:
android:background="@drawable/background"
更多信息请访问:
使用ScrollView作为顶级父级。
当您打开和关闭键盘时,它将自动上下滚动。您应该在AndroidManifest.xml
文件中的活动
上的窗口输入模式
设置中使用调整大小
选项。为什么不这样做
<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>
我曾经遇到过类似的问题,我的问题通过使用清单中的以下属性解决了,在声明活动的地方使用它
android:windowSoftInputMode="stateHidden|adjustResize|adjustPan"
我也有同样的问题
将外部布局背景添加到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\u weight=“1”
,不在xml文件中设置背景
我认为这对某人有帮助它没有用。背景也在缩小。我问的是背景图像。您没有提到图像及其收缩。adjustPan
应该完全满足您的需要(“活动的主窗口没有调整大小,以便为软键盘腾出空间。相反,窗口的内容会自动平移”),请参阅。你有什么问题?这个问题是重复的。为什么不把图像放在滚动视图的背景上呢?或者创建一个不带关系的imageview,填充整个屏幕空间,并将其设置为背景,因为它是RelatField布局。请附上您的问题的屏幕截图。我尝试了这个方法,但不起作用,因为在搜索中,我知道这是由于我的布局中的滚动视图。可能是因为您的AndroidManifest.xml,你能复制你的舱单代码并粘贴到这里吗?@ManojFegde你有一些进展吗?谢谢你的建议,很抱歉回复晚了。我试过你的答案。它可以工作,但我需要的是背景图像应该在键盘上向上移动,而不是隐藏在它后面。@ManojFegde你能用你想要什么的图像来编辑你的问题吗?如果我想在这里使用比例类型怎么办?因为对于凹口屏幕,图像正在拉伸。@Nil我不知道我在过去3年中没有在Android上工作过。顺便说一句,如果你不想在活动开始时隐藏键盘,你可以跳过stateHidden
part但是如何设置scaleType?