我有一个带有 EditText
的布局,如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
.....
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/some_button">
<EditText
android:id="@+id/some_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:hint="@string/some_text_hint"/>
.....
</RelativeLayout>
</RelativeLayout>
在横向模式下,此布局行为不一致。当在手机上显示时,some_text
将在聚焦时提取到全屏,这很好。但是,使用这种精确的布局,如果 some_text
显示在平板电脑上,则不会被提取。我也希望在平板电脑上将文本框提取到全屏。
我正在使用运行 Android 4.2.2 (API 17) 的 Nexus 5 手机和 Nexus 7 平板电脑模拟器预设进行测试。
更新: 在装有 Android 5.1 的 Samsung Galaxy Tab E 设备和运行 Android 5.1 的模拟器上,任何编辑文本都会重现此问题。
UPD2: 我创建了一个测试 empty project只是为了验证它确实是一个普通的文本框问题。它是。现在我的布局看起来像这样:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.myapplication.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="This should be fullscreen"/>
</RelativeLayout>
这是一个 Activity 代码,以防万一:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
还有一些图片,只是因为。这是布局在手机上正确呈现的方式(模拟和物理):
这是我在平板电脑上得到的:
最佳答案
经过一些研究和深入挖掘 Android 资源后,我发现提取文本编辑是输入法(键盘)的责任。无法强制键盘提取您的 View 。
我的 HTC 设备默认设置了 HTC 键盘,除非设置了 IME_FLAG_NO_EXTRACT_UI
,否则它会提取文本编辑。我的客户有三星平板电脑,默认情况下设置了三星键盘,不会提取三星平板电脑上的文本编辑。模拟器默认设置了 Google 键盘,它会在需要时提取文本编辑(在 5"模拟器上提取,在 5"设备上不提取)。
解决方案之一是寻找或实现键盘替换,在横向模式下提取文本编辑,然后推荐用户安装它。这是一个蹩脚的解决方案,因为没有人会这样做。
所以我最终只是确保我的 UI 不依赖于提取的文本编辑。在我的例子中,我的表单包含一个可滚动元素内的文本框。默认情况下,当文本框获得焦点时,android 在两个选项之间做出决定:
- 平移 View 并将文本框移动到可见位置。此行为称为
adjustPan
- 降低 View 的可用高度,从而缩小所有内容。此行为称为
adjustResize
。
如果您不弄乱选项,那么如果 View 中至少存在一个可滚动元素,Android 会选择第二个选项。否则它会平移 View 。您可以通过使用 adjustPan
或 adjustResize
值将 android:windowSoftInputMode
属性分配给 list 中的 Activity 来影响此决定。
最后,我选择了 adjustPan
行为并稍微重新安排了 View 中的元素,以确保平移 View 有意义并且看起来不奇怪。
关于android - EditText 不会在平板电脑上提取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36337297/