簡體   English   中英

出現鍵盤時使整個布局變小

[英]Making the whole layout smaller when the keyboard appears

我為創建新播放列表制作了一個新片段,但是當您聚焦 EditText 時,下面的按鈕不可見。 設置“ android:windowSoftInputMode="stateAlwaysHidden|adjustResize" ”或僅設置“ adjustResize ”並不能解決問題。

這是我的片段布局代碼:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/default_fullscreen_dialog"
    android:orientation="vertical"
    android:padding="24dp">


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/object_create_playlist_dialog_title"
            android:textColor="@color/white"
            android:textSize="20sp"
            android:textStyle="bold"
            android:layout_above="@id/dialog_cp_playlistname"/>

        <EditText
            android:id="@+id/dialog_cp_playlistname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:autofillHints=""
            android:gravity="center_horizontal"
            android:imeOptions="actionNext"
            android:inputType="text"
            android:lines="1"
            android:maxLines="1"
            android:textColor="@color/white"
            android:textCursorDrawable="@color/white_50"
            android:textSize="35sp"
            android:textStyle="bold"
            app:backgroundTint="@color/white_50"
            android:layout_centerVertical="true"/>

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:orientation="horizontal"
            android:stretchColumns="1,0"
            android:layout_below="@id/dialog_cp_playlistname"
            android:layout_centerVertical="true">

            <TableRow >

                <Button
                    android:id="@+id/dialog_cp_cancel"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/transparent"
                    android:fontFamily="sans-serif-thin"
                    android:gravity="end|center_vertical"
                    android:paddingStart="0dp"
                    android:paddingEnd="25dp"
                    android:text="@string/object_cancel"
                    android:textColor="@color/white_70"
                    android:textSize="13sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/dialog_cp_next"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/transparent"
                    android:fontFamily="sans-serif-thin"
                    android:gravity="start|center_vertical"
                    android:paddingStart="25dp"
                    android:paddingEnd="0dp"
                    android:text="@string/object_next"
                    android:textColor="@color/accent_color"
                    android:textSize="13sp"
                    android:textStyle="bold" />

            </TableRow>
        </TableLayout>
</RelativeLayout>

這就是我用於片段的樣式:

<style name="AppTheme.Dialog">
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:colorAccent">@color/accent_color</item>
    <item name="android:windowActionBar">true</item>
    <item name="android:windowAnimationStyle">@style/WindowAnimationStyle</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">#ffffff</item>
</style>

<style name="AppTheme.Dialog.CreatePlaylist">
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:colorAccent">@color/accent_color</item>
    <item name="android:windowActionBar">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:textSelectHandleLeft">@drawable/null_shape</item>
    <item name="android:textSelectHandleRight">@drawable/null_shape</item>
    <item name="android:textSelectHandle">@drawable/null_shape</item>
</style>

我的應用程序:

我的應用程序 我的應用程序與鍵盤

我想(Spotify):

我想 (Spotify) 我想用鍵盤(Spotify)

我會很高興得到答案。

為您在清單文件中保存此片段的活動添加以下屬性

android:windowSoftInputMode="stateUnchanged|adjustResize"

樣本

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity
        android:name=".MainActivity"
        android:windowSoftInputMode="stateUnchanged|adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

如果以不同的方式思考不起作用,請告訴我

編輯

腳步:

  1. 繼續使用android:windowSoftInputMode="stateUnchanged|adjustResize"如前所述
  2. 將您的片段布局包裝在ScrollViewNestedScrollView

看起來如何

這是一個與我一起工作的演示

主要活動

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

主活動布局

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

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

    <fragment
        android:id="@+id/fragment_container"
        android:name="com.example.android.androidxtest.TempFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

主片段

public class MainFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main, container, false);
    }
}

主片段布局

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="24dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/object_create_playlist_dialog_title"
            android:textSize="20sp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/dialog_cp_playlistname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:gravity="center_horizontal"
            android:imeOptions="actionNext"
            android:inputType="text"
            android:lines="1"
            android:maxLines="1"
            android:textSize="35sp"
            android:textStyle="bold" />

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:orientation="horizontal"
            android:stretchColumns="1,0">

            <TableRow>

                <Button
                    android:id="@+id/dialog_cp_cancel"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/object_cancel"
                    android:textSize="13sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/dialog_cp_next"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/object_next"
                    android:textSize="13sp"
                    android:textStyle="bold" />

            </TableRow>
        </TableLayout>
    </LinearLayout>

</ScrollView>

希望這有效!

我想,也許清單文件中存在問題。 檢查您的清單文件是否包含該片段。

現在洞答案。 不要使用windowTranslucentStatus在你的風格,而使用windowDrawsSystemBarBackgrounds並設置statusBarColor 例如:

 <style name="AppTheme.Dialog.CreatePlaylist">
        <item name="android:windowTranslucentStatus">false</item>     
        <item name="android:windowActionBar">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:statusBarColor">[some color]</item>
 </style>

順便說一下,您無需更改布局。

快樂編碼!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM