【问题标题】:Problems with orientation change and EditText on androidandroid上的方向更改和EditText问题
【发布时间】:2017-04-06 14:28:59
【问题描述】:

我有一个非常奇怪的问题:我有一个带有 EditText 的活动、三个按钮和两个用于处理方向更改的布局文件。当我以纵向启动应用程序并旋转设备时,android 会保留纵向布局,但如果我以横向启动应用程序,android 会加载适当的横向布局文件,但即使在旋转到纵向布局后也会再次保留横向布局。这只发生在包含 EditText 的活动中。怎么了?这是我的代码: 1)Manifest.xml:

  <activity
        android:name=".AnalyzeTextActivity"
        android:configChanges="orientation|screenSize|screenLayout"
        android:parentActivityName=".StartScreenActivity"
        android:windowSoftInputMode="adjustPan"
        />

2) 肖像

 <?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_analyze_text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ADD5F7"
    android:clickable="true"
    android:focusableInTouchMode="true"
    >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:gravity="start"
        android:id="@+id/edit_text_analysis"
        android:hint="@string/edit_text_hint"
        android:isScrollContainer="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:id="@+id/button_upload_file"
        android:background="@drawable/button_upload_large"
        android:onClick="onUploadClicked"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/button_upload_file"
        android:layout_marginTop="10dp"
        android:id="@+id/button_start_analysis"
        android:background="@drawable/button_start_large"
        android:onClick="onStartClicked"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/button_start_analysis"
        android:layout_marginTop="50dp"
        android:background="@drawable/button_clear_large"
        android:onClick="onClearClicked"/>

</RelativeLayout>

3) 风景

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_analyze_text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ADD5F7"
    android:clickable="true"
    android:focusableInTouchMode="true"
    >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:gravity="start"
        android:id="@+id/edit_text_analysis"
        android:hint="@string/edit_text_hint"
        android:isScrollContainer="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/edit_text_analysis"
        android:layout_toLeftOf="@+id/button_start_analysis"
        android:layout_marginRight="5dp"
        android:id="@+id/button_upload_file"
        android:layout_marginTop="2dp"
        android:background="@drawable/button_upload_small"
        android:onClick="onUploadClicked"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edit_text_analysis"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="2dp"
        android:id="@+id/button_start_analysis"
        android:background="@drawable/button_start_small"
        android:onClick="onStartClicked"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edit_text_analysis"
        android:layout_toRightOf="@+id/button_start_analysis"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="2dp"
        android:background="@drawable/button_clear_small"
        android:onClick="onClearClicked"/>

</RelativeLayout>

【问题讨论】:

  • 你能分享你的布局文件吗?
  • @Volodymyr Khodonovych 完成
  • 检查请在下面回答
  • 您能否分享一下您放置纵向和横向布局的文件夹名称
  • @Sumit Pathak 布局和布局土地

标签: android xml android-layout


【解决方案1】:

问题出在清单文件中

android:configChanges="orientation|screenSize|screenLayout"

默认配置更改由android处理,当你在manifest文件中定义android:configChanges然后你告诉android你将处理你活动中括号中的所有confiChanges,有一个特殊的方法

 @Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

要解决您的问题,只需从清单中删除 orientation

android:configChanges="screenSize|screenLayout"

【讨论】:

  • @ Volodymyr Khodonovych 对,当我从清单中删除方向时,android 会加载正确的布局文件,但我需要在清单文件中使用方向以在更改方向时保留 ProgressDialog。还有其他建议吗?
  • 检查这个问题有几个可能的方法stackoverflow.com/questions/1111980/…
【解决方案2】:

中删除android:configChanges="screenSize|screenLayout"
  `<activity
    android:name=".AnalyzeTextActivity"
    android:configChanges="orientation|screenSize|screenLayout"
    android:parentActivityName=".StartScreenActivity"
    android:windowSoftInputMode="adjustPan"
    />`

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多