gpt4 book ai didi

android - Android中的透明状态栏导致EditText被键盘隐藏

转载 作者:太空狗 更新时间:2023-10-29 13:18:37 25 4
gpt4 key购买 nike

我按照 Google 的 Material 设计指南制作了一个 Android 应用程序。所以,我实现了最新的支持库(我的最小 Sdk 值为 14)

现在,我面临一个问题:我使用自定义工具栏(在 XML 文件中定义)使状态栏透明。一切正常,除了 FloatingActionButton(来自 https://github.com/makovkastar/FloatingActionButton)跳过 XML 定义的布局边距。 Idem,我的应用程序的一个 UI 使用 TextView 来登录用户:一旦聚焦,它们就会被软键盘隐藏。

这两个问题是在将主要样式从“AppCompat.DarkActionBar”替换为“AppCompat.NoActionBar”(并将项目“android:windowTranslucentStatus”设置为“true”)后出现的。

我尝试在我的 list 中将 windowSoftInputMode 与“adjustPan|adjustResize”一起使用,但没有任何变化。将“ScrollView”添加到内部带有“EditText”的布局也不能解决我的问题。

我的英语不好,我知道图片胜于文字,所以这是我想做的以及正在发生的事情:

(图片链接)http://i.stack.imgur.com/fIRCl.png

有什么想法吗?感谢阅读!


EDIT : I solved my problem with the FloatingButton, just need to add this line to the component's XML declaration : android:layout_alignParentBottom="true"

现在唯一的问题是键盘隐藏了EditText。这是我的代码:

.

activity_main.xml

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="7dp"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<!-- My toolbar -->
<include
android:id="@+id/tool_bar"
layout="@layout/toolbar_design">
</include>

<!-- Fragment's place -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true">
</FrameLayout> <!-- Open drawer bug fix -->

</LinearLayout>

<!-- Sliding Drawer -->
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@null"
android:clipToPadding="false"
android:overScrollFooter="@android:color/transparent"
android:dividerHeight="0dp"
android:background="@color/drawer_background"/>

</android.support.v4.widget.DrawerLayout>

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/md_blue_800"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:elevation="4dp" >
</android.support.v7.widget.Toolbar>

fragment_profile.xml(带有 EditText 的 View )

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/container_dropshadow">

<!-- Top fragment's Image -->
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="centerCrop"
android:src="@drawable/top_image"/>

</LinearLayout>

<!-- Scrollview in case of screen's height is too small -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:id="@+id/rlFillProfile">

<!-- long long text ( 9 lines ) -->
<TextView
android:text="@string/text_edit_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:id="@+id/tvEditProfile" />

<!-- User ID EditText -->
<com.rengwuxian.materialedittext.MaterialEditText
android:layout_below="@id/tvEditProfile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:maxLines="1"
android:singleLine="true"
android:maxEms="10"
android:hint="Your user ID"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:id="@+id/etUserID"
android:layout_alignLeft="@+id/etUserPassword"
android:layout_alignStart="@+id/etUserPassword" />

<!-- User ID TextView -->
<TextView
android:id="@+id/tvUserID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Identifiant :"
android:textSize="18dp"
android:layout_marginBottom="16dp"
android:layout_alignBottom="@id/etUserID"
android:layout_alignRight="@+id/tvUserPassword"
android:layout_alignEnd="@+id/tvUserPassword" />

<!-- User password TextView -->
<com.rengwuxian.materialedittext.MaterialEditText
android:layout_below="@id/etUserID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:maxLines="1"
android:singleLine="true"
android:maxEms="10"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:inputType="textPassword"
android:id="@+id/etUserPassword"
android:hint="******"
android:layout_toRightOf="@+id/tvUserPassword"
android:layout_toEndOf="@+id/tvUserPassword" />

<!-- User password TextView -->
<TextView
android:id="@+id/tvUserPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mot de passe :"
android:textSize="18dp"
android:layout_alignBottom="@+id/etUserPassword"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<!-- Button to get user's touch event -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Continuer"
android:id="@+id/button_disconnect"
android:layout_below="@id/etUserPassword"
android:layout_centerHorizontal="true" />

</RelativeLayout>
</ScrollView>
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" ... >

<!-- Permissions -->
...

<application
... >

<activity
android:name="..."
android:label="@string/app_name"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"
android:windowSoftInputMode="adjustResize|stateHidden">

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

在我的 MainActivity 中,我有 DrawerLayout 和一些与工具栏/布局无关的代码行,除了:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

...

toolbar = (Toolbar) findViewById(R.id.tool_bar);
toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
setSupportActionBar(toolbar);

// enabling action bar app icon and behaving it as toggle button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));

...
}


// A method to find height of the status bar
// Source : an Stackoverflow's answer
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}

最佳答案

好的,终于找到解决办法了。在 Fragment 中使用此代码可以解决所有问题。

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

顺便说一下,使用 AppCompatActivity 而不是 ActionBarActivity(支持 V7),我不确定它会改变什么,但以防万一......

(来自:这个答案https://stackoverflow.com/a/28012530/5200029)

关于android - Android中的透明状态栏导致EditText被键盘隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31867595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com