簡體   English   中英

在Android中,如何將視圖附加到軟鍵盤?

[英]In Android, how do I attach a view to the soft keyboard?

我所進行的是一個照片上傳活動,其中照片為主要背景,底部為edittext和一組按鈕。 單擊編輯文本時,我希望軟鍵盤將編輯文本和按鈕向上推(以便它們仍然可見),並保持背景圖像不變。 但是,現在它僅將其向上推足以看到edittext的第一行,而我的所有按鈕都被隱藏了。

我基本上想以某種方式將視圖附加到軟鍵盤上,而將其余的活動保留下來。 我試過在清單中設置windowSoftInputMode標志,但是沒有一個選項會產生我想要的效果(我不想調整布局的大小,並且平移似乎沒有任何影響)。 我如何實現這樣的目標?

不幸的是,在當前的SDK中,當軟鍵盤顯示並消失時,視圖的行為方式對系統有些擺布。 實現所需行為的最佳方法是在窗口上保留默認輸入模式,並確保視圖中有一個可滾動元素。 請記住,隱藏鍵盤時不需要滾動此元素(內容小於滾動視圖),但是當鍵盤顯示時,它將折疊可滾動視圖的大小並保留其他所有內容。

這是一個例子。 假設這是您的res / layout / main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <LinearLayout
    android:id="@+id/control"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="vertical">
    <EditText
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"/>
    <Button
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"
      android:text="A Button"/>
  </LinearLayout>
  <ScrollView
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:layout_above="@id/control">
  </ScrollView>
</RelativeLayout>

嘗試將其作為基本Activity的根布局,您會看到整個LinearLayout隨鍵盤上下滑動,因為Android正在調整ScrollView的視口的大小,而不是向上滑動視圖就足以顯示聚焦的輸入元素(您現在看到的)。

暫無
暫無

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

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