2015-03-03 29 views
0

我有一個DialogFragment具有以下佈局:使佈局總是可見的一部分時,會顯示鍵盤

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
android:background="@drawable/rounded_rect_light"> 

<RelativeLayout 
    android:id="@+id/create_note_top_panel" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:background="#f2f3f5"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:text="Cancel"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Title" 
     android:gravity="center" 
     android:layout_centerInParent="true"/> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:text="Send"/> 
</RelativeLayout> 

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dp"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <ImageView 
      android:id="@+id/quote_image" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:layout_alignParentTop="true" 
      android:src="@drawable/edu4_create_note_quotes"/> 

     <TextView 
      android:id="@+id/quote_text" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/quote_image" 
      android:scrollbars="vertical" 
      android:fadeScrollbars="false" 
      android:text="The issue is that soft keyboard is very tricky and this\n quesakdf\njk\nlsd\nfj\nklsdjfklsdjfkl jsdklfj skldjkljfklsdjklfjsdklfjsdkljfkls" 
      android:maxLines="5"/> 

     <EditText 
      android:layout_width="match_parent" 
android:layout_height="150dp" 
      android:layout_below="@id/quote_text" 
      android:layout_margin="10dp" 
      android:gravity="top|left" 
      android:background="@drawable/rounded_rect_background_white" 
      android:padding="5dp"/> 
    </RelativeLayout> 
</ScrollView> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:paddingBottom="15dp" 
    android:layout_marginLeft="10dp"> 

    <TextView 
     android:id="@+id/visibleTo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:layout_gravity="center_vertical" 
     android:text="Publish"/> 

    <ToggleButton 
     android:id="@+id/switchPublishToCourse" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="20dp"/> 
</LinearLayout> 

我想的是,鍵盤打開時,頂部面板(android:id="@+id/create_note_top_panel")會在頂部可見,並且對話框(它的背景)將位於鍵盤上方,底部的TextViewToggleButton將可見,其餘佈局將可滾動。

+0

這也許SO答案將滿足您的需求。如何在Android中捕獲'虛擬鍵盤顯示/隱藏'事件? http://stackoverflow.com/a/7423586/1027550 – MRodrigues 2015-03-03 12:29:56

+0

謝謝,但我不明白這是如何幫助我太多 – piojo 2015-03-03 13:42:26

+0

我以爲你聽到鍵盤被打開的事件有困難。 – MRodrigues 2015-03-03 14:02:36

回答

1

找到答案:將android:layout_weight="0" to the parts that you want to stay visible and android:layout_weight =「1」添加到可調整大小的那個。 以下是完整的XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
android:background="@drawable/rounded_rect_light"> 

<RelativeLayout 
    android:id="@+id/create_note_top_panel" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:background="#f2f3f5"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:text="Cancel"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Title" 
     android:gravity="center" 
     android:layout_centerInParent="true"/> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:text="Send"/> 
</RelativeLayout> 

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:layout_marginTop="10dp"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <ImageView 
      android:id="@+id/quote_image" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:layout_alignParentTop="true" 
      android:src="@drawable/edu4_create_note_quotes"/> 

     <TextView 
      android:id="@+id/quote_text" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:scrollbars="vertical" 
      android:fadeScrollbars="false" 
      android:layout_toRightOf="@id/quote_image" 
      android:text="The issue is that soft keyboard is very tricky and this\n quesakdf\njk\nlsd\nfj\nklsdjfklsdjfkl jsdklfj skldjkljfklsdjklfjsdklfjsdkljfkls\nend of tex" 
      android:maxLines="5"/> 

     <EditText 
      android:id="@+id/create_note_text" 
      android:layout_width="match_parent" 
      android:layout_height="150dp" 
      android:layout_below="@id/quote_text" 
      android:layout_margin="10dp" 
      android:gravity="top|left" 
      android:background="@drawable/rounded_rect_background_white" 
      android:padding="5dp"/> 
    </RelativeLayout> 
</ScrollView> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_weight="0" 
    android:gravity="center_vertical" 
    android:layout_marginLeft="10dp" 
    android:layout_marginBottom="10dp"> 

    <TextView 
     android:id="@+id/visibleTo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:layout_gravity="center_vertical" 
     android:text="Publish"/> 

    <ToggleButton 
     android:id="@+id/switchPublishToCourse" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="20dp"/> 
</LinearLayout> 

,並在Fragment類:

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); 
} 
相關問題