Android-在可见虚拟键盘时努力布局我的应用程序 [英] Android - Struggiling to layout my application when virtual keyboard is visible
问题描述
我正在为我的Android
应用程序困扰一个非常烦人的问题.
我创建了一个Activity
来注册我的应用.
I'm struggling in a very annoying problem for my Android
App.
I created an Activity
for registering to my App.
这是Activity
xml文件:
This is the Activity
xml file:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="myapplication.MainActivity">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="?attr/actionBarTheme"
android:minHeight="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/appTitleBar"
android:elevation="4dp">
<TextView
android:text="@string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/toolbarText"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:textColor="?android:attr/textColorPrimaryInverse" />
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="?attr/actionBarTheme"
android:minHeight="?attr/actionBarSize"
android:id="@+id/activityTitleBar"
android:layout_below="@+id/appTitleBar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:elevation="4dp">
<TextView
android:text="@string/signinActivityTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:textColor="?android:attr/textColorPrimaryInverse" />
</android.support.v7.widget.Toolbar>
<TextView
android:text="Welcome. bla bla bla bla bla bla bla bla bla bla"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/activityTitleBar"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp"
android:id="@+id/textView3"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textAlignment="center" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:layout_below="@+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="96dp"
android:id="@+id/editText3"
android:hint="password" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:layout_below="@+id/editText3"
android:layout_alignRight="@+id/editText3"
android:layout_alignEnd="@+id/editText3"
android:layout_marginTop="49dp"
android:id="@+id/editText4"
android:hint="confirm" />
<Button
android:text="SIGN IN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="22dp"
android:id="@+id/button2"
android:layout_alignRight="@+id/editText4"
android:layout_alignEnd="@+id/editText4"
android:layout_alignLeft="@+id/editText4"
android:layout_alignStart="@+id/editText4" />
</RelativeLayout>
如您所见ת我正在使用两个Toolbars
(我选择了一个NoActionBar
主题),一个用于应用程序名称,另一个用于Activity
标题.
As you can seeת I'm using two Toolbars
(I've selected a NoActionBar
theme), one for the App name and one for the Activity
title.
然后我放置了一个TextView
,其中包含欢迎消息和该应用程序的介绍.
Then I placed a TextView
that contains a welcome message and an introduction of the App.
最后,有一个登录表单,由两个EditText
(用于密码和密码确认)和一个Button
组成,它们提交密码以供以后处理.
Finally, there is the sign in form, composed of two EditText
(for password and password confirmation) and one Button
that submit the passwords in order to be processed later.
在这里,它是在正常状态"下出现的方式:
这是打开虚拟键盘时的程序:
这是问题所在:
第一个问题是工具栏消失了.而且我不想要它.
第二个问题是,我希望所有表单(两个编辑文本和Button
)必须位于键盘上方.相反,只有第一个编辑文本在上方.
此外,欢迎TextView
也必须消失.
First problem is, that the toolbars disappear. And I don't want it.
Second problem is, that I want that all the form (the two edit-text and the Button
) must be above the keyboard. Instead, only the first edit-text is above.
In addition the welcome TextView
has to desappear.
实际上,我希望在虚拟键盘启动时我的应用程序像这样:
我尝试过玩
<activity android:windowSoftInputMode="adjustResize">
推荐答案
我通常使用的是ScrollView
:
如下所示在Layout
中定义ScroolView
(或仅复制粘贴代码):
Define the ScroolView
in your Layout
like below (or just copy paste the code):
<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"
tools:context="com.igor.test.MainActivity">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="?attr/actionBarTheme"
android:minHeight="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/appTitleBar"
android:elevation="4dp">
<TextView
android:text="@string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/toolbarText"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:textColor="?android:attr/textColorPrimaryInverse" />
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="?attr/actionBarTheme"
android:minHeight="?attr/actionBarSize"
android:id="@+id/activityTitleBar"
android:layout_below="@+id/appTitleBar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:elevation="4dp">
<TextView
android:text="signinActivityTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:textColor="?android:attr/textColorPrimaryInverse" />
</android.support.v7.widget.Toolbar>
<ScrollView
android:id="@+id/scroolView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/activityTitleBar">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:text="Welcome. bla bla bla bla bla bla bla bla bla bla"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp"
android:id="@+id/textView3"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textAlignment="center" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:layout_below="@+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="96dp"
android:id="@+id/editText3"
android:hint="password" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:layout_below="@+id/editText3"
android:layout_alignRight="@+id/editText3"
android:layout_alignEnd="@+id/editText3"
android:id="@+id/editText4"
android:hint="confirm" />
<Button
android:text="SIGN IN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/button2"
android:layout_below="@+id/editText4"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
在您的Activity
中,设置ScrollView
和主RelativeLayout
的引用
还要在主RelativeLayout
上设置ViewTreeObserver.OnGlobalLayoutListener
并实现onGlobalLayout()
方法.
In your Activity
set the referance of the ScrollView
and the main RelativeLayout
Also set ViewTreeObserver.OnGlobalLayoutListener
on your main RelativeLayout
and implements the onGlobalLayout()
method.
看下面的例子:
public class MainActivity extends AppCompatActivity implements ViewTreeObserver.OnGlobalLayoutListener {
private ScrollView scrollView;
private RelativeLayout activity_main_RL;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollView = (ScrollView) findViewById(R.id.scroolView);
activity_main_RL = (RelativeLayout) findViewById(R.id.activity_main);
activity_main_RL.getViewTreeObserver().addOnGlobalLayoutListener(this);
}
@Override
public void onGlobalLayout() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
int heightDiff = activity_main_RL.getRootView().getHeight() - activity_main_RL.getHeight();
if (heightDiff > 100) {
scrollView.scrollBy(0, scrollView.getBottom());
}
}
}, 500);
}
}
这篇关于Android-在可见虚拟键盘时努力布局我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!